Date range C #

2

I have a table in the database named Quota, this quota has two columns one for start_date and end_date, so far so good, now I have another table called Order that also has start_date and date_fim, these tables are related 1 for N, one quota can have N Orders, I created a validation to not let the user create an order that is not greater or smaller than the date of the registered quota, every order must be within the range registered in the quota, my problem is that I can not leave the order too user to enter an order that enters within the range of another order, example:

Date Quota: start_date 10/08/2017 and end_data: 08/20/2017

1: Order date: start_date 11/08/2017 and end_data: 08/15/2017

Here's the problem:

2: Order date: start_date 08/13/2017 and end_date: 08/17/2017

The date_inicio 08/13/2017 of this last order has entered the range of 1 Order: date_inicio 11/08/2017 and date_fim: 08/15/2017 this can not occur.

Is there any method or solution to this, suggestion for improvement? Here is the method with some validations I have made.

 public void ValidarDataDaOrdem(decimal idCota, DateTime dataInicio, DateTime dataFim)
    {
        var cota = Context.Cota.Include("Ordem").Where(e => e.IdCota == idCota).FirstOrDefault();
        var ordens = cota.Ordem.ToList();

        if (dataInicio < cota.dataInicioCota)
        {
            throw new InfoException("Data Inico tem que ser maior que a data inicio da cota");
        }
        if (dataFim > cota.DataFimCota)
        {
            throw new InfoException("Data Final tem que ser menor que a data final da cota");
        }

        foreach (var item in ordens)
        {
            if (item.DataInicioOrdem == dataInicio && item.DataInicioOrdem == DataFim)
            {
                throw new InfoException("Ja existe solicitação cadastrada para esse periodo");
            }
            if (item.DataInicioOrdem == dataInicio || item.DataInicioOrdem == DataFim)
            {
                throw new InfoException("Ja existe solicitação cadastrada para essa data");
            }
        }
    }
    
asked by anonymous 11.08.2017 / 16:07

2 answers

1

I recommend replacing this snippet:

foreach (var item in ordens)
{
    if (item.DataInicioOrdem == dataInicio && item.DataInicioOrdem == DataFim)
    {
        throw new InfoException("Ja existe solicitação cadastrada para esse periodo");
    }
    if (item.DataInicioOrdem == dataInicio || item.DataInicioOrdem == DataFim)
    {
        throw new InfoException("Ja existe solicitação cadastrada para essa data");
    }
}

By:

var ordemEmChoque = ordens.FirstOrDefault(o =>
    (o.DataInicioOrdem <= dataInicio && o.DataFimOrdem >= dataInicio) ||
    (o.DataInicioOrdem <= dataFim && o.DataFimOrdem >= dataInicio)
);
if (ordemEmChoque != null) {
    throw new InfoException("Já existe uma solicitação cadastrada com choques " +
        "de data para a nova solicitação. A solicitação já existente é a de número" + foo);
}

Where foo is an identity or code that identifies an existing order.

    
11.08.2017 / 17:13
1

I imagine you are validating the dates of entry into the client, however, being a conditional information of the functionality, it should validate again on the server.

I did not test effectively, but I think something like this might help you:

public void ValidarDataDaOrdem(decimal idCota, DateTime dataInicio, DateTime dataFim)
{
    var cota = Context.Cota.Include("Ordem").Where(e => e.IdCota == idCota).FirstOrDefault();

    if(dataInicio > dataFim || dataInicio < cota.DataInicio  || dataInicio > cota.DataFim || dataFinal > cota.DataFinal || dataFinal < cota.DataInicio)
        throw new InfoException($"Datas informadas fora do intervalo permitido [De: {cota.DataInicio} à {cota.DataFim}");

    if(cota.Ordem.Where(item=> dataInicio >= item.dataInicioOrdem && dataInicio <= item.dataFimOrdem).count() > 0)
        throw new InfoException("Já existe uma solicitação cadastrada para este período.");
}

You could search for linq and lambda to make better use of filter issues and so on. Improve the queries in a general way. It is very simple and productive. (=

    
11.08.2017 / 17:01