Date range C#

Asked

Viewed 1,697 times

2

I have a table in the bank called Quota, this quota has two columns one for data_start and data_end, so far so good, now I have another table called Order that also has data_start and data_end, these tables are related 1 to N, a quota can have N Orders, I created a validation to not let the user create an order that is not larger or smaller than the date of the registered quota, all order must be within the range registered in Quota, my problem is that I can also not let the user register an order that enters within the range of other order, example:

Date of Quota: starting date 10/08/2017 and enddate: 20/08/2017

1: Order date: starting date 11/08/2017 and ending date: 15/08/2017

Here is the problem:

2: Date of the order: starting date 13/08/2017 and ending date: 17/08/2017

The starting date 13/08/2017 of this last order entered the interval of 1 Order: start_date 11/08/2017 and end_date: 08/15/2017 this cannot occur.

Is there any method or solution for this, suggested improvement ? Here’s the method with some validations I 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");
            }
        }
    }

2 answers

1

I imagine that you are validating the dates of entry into the client, still, being a conditional information of the functionality, should validate again in the server.

I haven’t tested it 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 by Inline and Lambda to better utilize the filter questions and etc. Improve queries in a general way. It’s very simple and productive. (=

1


I recommend replacing this excerpt:

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");
    }
}

For:

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.