private async Task <bool> RepeatedEventExists(long groupId, EventRepeat @event) { var @group = await _context.Groups.FindAsync(groupId); if (@group.GroupRepeatedEvents.Where(gre => gre.EventRepeatId == @event.EventRepeatId).Count() == 0) { return(false); } return(true); }
public async Task <bool> UpdateDBRepeatedEventAsync(long groupId, EventRepeat @event) { _context.Entry(@event).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch { if (!await RepeatedEventExists(groupId, @event)) { return(false); } else { throw; } } return(true); }
public async Task CreateEventAsync(RepeatedEvent @event, long userId, long groupId) { DateTime repeatDate = @event.Event.Start; DateTime endDate; switch (@event.EventRepeat.ERepeat) { case Repeat.Weekly: endDate = repeatDate.AddYears(3); var weeklyEventRepeat = new EventRepeat() { EventName = @event.Event.EventName, ERepeat = @event.EventRepeat.ERepeat, Location = @event.Event.Location, Description = @event.Event.Description, DayOfWeek = @event.Event.Start.DayOfWeek, FirstEventDate = @event.Event.Start, EndEventDate = endDate }; _context.EventRepeats.Add(weeklyEventRepeat); await _context.SaveChangesAsync(); Console.WriteLine(weeklyEventRepeat.EventRepeatId); while (repeatDate < endDate) { var weeklyEvent = new Event() { EventName = @event.Event.EventName, Start = repeatDate, Food = @event.Event.Food, Cost = @event.Event.Cost, Location = @event.Event.Location, Description = @event.Event.Description, ERepeat = @event.EventRepeat.ERepeat }; await CreateIndividualEventAsync(weeklyEvent, userId, groupId, weeklyEventRepeat.EventRepeatId); repeatDate = repeatDate.AddDays(7); } var weeklyGroupRepeatedEvent = new GroupRepeatedEvent { EventRepeatId = weeklyEventRepeat.EventRepeatId, GroupId = groupId }; _context.GroupRepeatedEvents.Add(weeklyGroupRepeatedEvent); await _context.SaveChangesAsync(); break; case Repeat.Monthly: endDate = repeatDate.AddYears(5); var monthlyEventRepeat = new EventRepeat() { EventName = @event.Event.EventName, ERepeat = @event.EventRepeat.ERepeat, Location = @event.Event.Location, Description = @event.Event.Description, DayOfMonth = @event.Event.Start.Day, FirstEventDate = @event.Event.Start, EndEventDate = endDate }; _context.EventRepeats.Add(monthlyEventRepeat); await _context.SaveChangesAsync(); while (repeatDate < endDate) { var monthlyEvent = new Event() { EventName = @event.Event.EventName, Start = repeatDate, Food = @event.Event.Food, Cost = @event.Event.Cost, Location = @event.Event.Location, Description = @event.Event.Description, ERepeat = @event.EventRepeat.ERepeat }; await CreateIndividualEventAsync(monthlyEvent, userId, groupId, monthlyEventRepeat.EventRepeatId); repeatDate = repeatDate.AddMonths(1); } var monthlyGroupRepeatedEvent = new GroupRepeatedEvent { EventRepeatId = monthlyEventRepeat.EventRepeatId, GroupId = groupId }; _context.GroupRepeatedEvents.Add(monthlyGroupRepeatedEvent); await _context.SaveChangesAsync(); break; case Repeat.Yearly: endDate = repeatDate.AddYears(100); var annualEventRepeat = new EventRepeat() { EventName = @event.Event.EventName, ERepeat = @event.EventRepeat.ERepeat, Location = @event.Event.Location, Description = @event.Event.Description, DayOfMonth = @event.Event.Start.Day, MonthOfYear = (MonthOfYear)@event.Event.Start.Month, FirstEventDate = @event.Event.Start, EndEventDate = endDate }; _context.EventRepeats.Add(annualEventRepeat); await _context.SaveChangesAsync(); while (repeatDate < endDate) { var annualEvent = new Event() { EventName = @event.Event.EventName, Start = repeatDate, Food = @event.Event.Food, Cost = @event.Event.Cost, Location = @event.Event.Location, Description = @event.Event.Description, ERepeat = @event.EventRepeat.ERepeat }; await CreateIndividualEventAsync(annualEvent, userId, groupId, annualEventRepeat.EventRepeatId); repeatDate = repeatDate.AddYears(1); } var annualGroupRepeatedEvent = new GroupRepeatedEvent { EventRepeatId = annualEventRepeat.EventRepeatId, GroupId = groupId }; _context.GroupRepeatedEvents.Add(annualGroupRepeatedEvent); await _context.SaveChangesAsync(); break; case Repeat.Once: var onceEventRepeat = new EventRepeat() { EventName = @event.Event.EventName, ERepeat = @event.EventRepeat.ERepeat, Location = @event.Event.Location, Description = @event.Event.Description, FirstEventDate = @event.Event.Start, EndEventDate = @event.Event.Start }; _context.EventRepeats.Add(onceEventRepeat); await _context.SaveChangesAsync(); var IndividualEvent = new Event() { EventName = @event.Event.EventName, Start = repeatDate, Food = @event.Event.Food, Cost = @event.Event.Cost, Location = @event.Event.Location, Description = @event.Event.Description, ERepeat = @event.EventRepeat.ERepeat }; await CreateIndividualEventAsync(IndividualEvent, userId, groupId, onceEventRepeat.EventRepeatId); var onceGroupRepeatedEvent = new GroupRepeatedEvent { EventRepeatId = onceEventRepeat.EventRepeatId, GroupId = groupId }; _context.GroupRepeatedEvents.Add(onceGroupRepeatedEvent); await _context.SaveChangesAsync(); break; default: Console.WriteLine("Something went wrong."); break; } }