Search code examples
c#asp.netasp.net-mvcdateaction-filter

What is the best way to set time boundries for user to input data? (asp.net mvc)


I'm actually wrapping my head around the problem of time. I wish to allow the user to input data between Monday-Wednesday (Monday 00:00 to Wednesday 23:59) each week just once AND create a unique id that would represent each week.

I need this id later, so the user can't insert another piece of the same data until next week opening.

At first, I went for DateTime.Today but then realised that it doesn't prevent the user from inserting another piece of data the next (or the other) day.

Now I'm thinking of creating an action filter that would block out the user if he has already signed in data for the week and take it on the next Monday. I'd need a function that resets the flag on Monday 00:00 and raise flag on Thursday 00:00 (if user hasn't signed the data in) which atm seems a lot of time-control work (that's why I went for unique id 1st) and setting on/off the action filter (which I don't know if is really possible or how to do it).

I think that this is a bad road to follow meaning there could be another way to achieve my goal. Or there might even be some kind of solution already. That's why I ask you here, hoping for inspiration.

Thanks!

EDIT:

All of the above concerns raport_id as unique id.

db classes:

-raport

public class Raport
    {
[DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public string raport_id { get; set; } //id raportu
        public string userID { get; set; }

        public DateTime creation_time { get; set; }

        public int isMinus { get; set; }

        public DateTime last_modyfication { get; set; }


        public virtual ICollection<ThrashType> ThrashType { get; set; } 


        public virtual UserFrontInfo UserFrontInfo { get; set; }       
    }

-thrash type

public class ThrashType
{

    [Key] 
    public int IdTrash { get; set; }
    public string raport_id { get; set; } 
    public string ClassName { get; set; } 

    public bool isNegative { get; set; }
    public int Quantity { get; set; } 

    public string Information { get; set; } 

    public virtual Raport Raport { get; set; }   

}

creating raport_id:

            var datePart = DateTime.Today.ToString().Replace(".", "").Replace(":","").Replace(" ","");
            var namePart = User.Identity.Name.ToString();

            var nameShort = namePart.Remove((namePart.Length)-13, 12); 

            newRaport.raport_id = datePart + nameShort;

Solution

  • It seems that you are twisting the problem for yourself. From your text, I understand this:

    You want to prevent user from adding a row to your table more than once a week.

    You can check the week by:

    cal.GetWeekOfYear(date1, dfi.CalendarWeekRule,dfi.FirstDayOfWeek)
    

    It is also easy to check your new row to see if there is another row in that week.

    i.e: if you use sqlserver (t-sql) you have something like this

    SELECT DATEPART(wk, GETDATE())
    

    This is very important:

    Don't set a flag for something like this. Just check your table and see if a row exists for that person in that week and you are good to go.

    If your concern is the performance, by adding a flag you will do the same process to call database, believe me this is not a scenario that you need to implement a job and...