0

I have an application written in c# to allow users to manage events, when adding I allow the users to add a repeat type of Daily,Monthly,Weekly and another option to select how many times it should be repeated or till what day it should be repeated.

Now when displaying the events I need to show all the events that are available for a week.

EX: The user adds an event today and that event repeats daily for 30 days

When displaying the events I query for events using

var events = db.Evnt.Where(l => l.repeatTill >= weekEnd).ToList()

Once I get the results I need to generate the repeating events and display them to the user, How can I achieve this

Final output should be something like this

E1
11/24/2019 10:00 - 11/24/2019 11:00
E1
11/25/2019 10:00 - 11/25/2019 11:00
E1
11/26/2019 10:00 - 11/26/2019 11:00
E1
11/27/2019 10:00 - 11/27/2019 11:00
E1
11/28/2019 10:00 - 11/28/2019 11:00
E1
11/29/2019 10:00 - 11/29/2019 11:00
E1
11/30/2019 10:00 - 11/30/2019 11:00
E1
12/01/2019 10:00 - 12/01/2019 11:00

If the user filters from 11/24/2019 - 12/08/2019 the output should be

E1
11/24/2019 10:00 - 11/24/2019 11:00
E1
11/25/2019 10:00 - 11/25/2019 11:00
E1
11/26/2019 10:00 - 11/26/2019 11:00
E1
11/27/2019 10:00 - 11/27/2019 11:00
E1
11/28/2019 10:00 - 11/28/2019 11:00
E1
11/29/2019 10:00 - 11/29/2019 11:00
E1
11/30/2019 10:00 - 11/30/2019 11:00
E1
12/01/2019 10:00 - 12/01/2019 11:00
E1
12/02/2019 10:00 - 12/02/2019 11:00
E1
12/03/2019 10:00 - 12/03/2019 11:00
E1
12/04/2019 10:00 - 12/04/2019 11:00
E1
12/05/2019 10:00 - 12/05/2019 11:00
E1
12/06/2019 10:00 - 12/06/2019 11:00
E1
12/07/2019 10:00 - 12/07/2019 11:00
E1
12/08/2019 10:00 - 12/08/2019 11:00

How can I achieve this?

Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
  • Do the repeated events not physicly exist in your database ? Do you store one event with the information "it repeats", or do you generate 30 events ? – Holger Nov 29 '19 at 11:32
  • @Holger The repeated events are not stored in the database, it only stores a flag on how often it repeats – mike oconner Nov 29 '19 at 12:34
  • 1
    Not a good design, since you have not the possibility to delete one of the thirty events, or edit just a single date. You need the generation process on each read, instead of only once, at creation time. But however in this case the definition of your event class would be helpfull. This can be quiet complex if you implement things like "10 times every second week", "Quarterly", "Monday-Friday except public holidays " etc. This byitself is a huge design job. Requesting the data from it comes later. – Holger Nov 29 '19 at 12:41
  • @Holger Is it okay to add the event dates to a separate table using a FK, I didn't do it because if the user needs the event to go on forever the database will keep growing. Should I go back to the initial design. PS: this is for a Course Work – mike oconner Nov 29 '19 at 15:38
  • If you are free in design, and you can justify your decision, beside the possibility of more complex rules, it's just easier to handle. Otherwise you had to generate all the events by any given rule, in every query. And a user is unable to edit "this week, we meet 1hour earlier". What do you do, if one wants to edit an event, and some of the events did already occur ? I would rather forbid the eternity - endless repetition is not realistic, cause life is not endless ;-) We do not save a few bytes of hard disk space any more, it's not 1990. – Holger Nov 29 '19 at 16:12
  • @Holger That is true, i wont be able to handle complex rules like that using my current design, should i create an event for each date with the same details or should i have a table mapped by a foreign key that stores all the dates. Also the repetition part should i have a limit on how long the events can be repeated for. – mike oconner Nov 29 '19 at 17:24
  • I would create copy of the initial event. In this way you are able to modify individual location. Move a single event to another location, or move the time a little etc. You know the Olympic games. It's every 4 years, but not exactly the same calendar day, not always the same location, etc. Manual changes to events should be possible – Holger Nov 29 '19 at 22:06
  • @Holger Thanks, I will do that way. The changes you said are again not possible even if I store the dates in a separate table. Also is it okay if I give a maximum time limit of 1 year for event that repeat daily? – mike oconner Nov 29 '19 at 23:19
  • Make your own decisions, so you can defend them against others. If your events are "Watch Game of Thrones, Mondays 9pm" 1 year might be enough, if you want to store the Olympic Games every 4 years, than it's not. For daily it should be enough anyway, but for daily things like "brush your teeth" I don't need a calendar. Your teacher will be pleased if he sees you had some thoughts, by what you do. – Holger Nov 30 '19 at 10:24

2 Answers2

1

You could calculate the difference between the date range from the available data and then run a loop to generate the events for that date range. Make sure to change the start date of the event to the start date of the date ranges start date that was used when calculating the time difference.

Rasheen Ruwisha
  • 135
  • 3
  • 10
0

So, from your data you know (or can calculate) the date range between the first instance of the event and the final one? Then you just need to calculate the number of days between those dates..

Then this answer should help: Calculate difference between two dates (number of days)?

Matt B
  • 176
  • 1
  • 10