public void Add(IEvent eventModel) { using (var context = new ClassBookingContext()) { if (ServiceHelper.IsRoomBusy(eventModel)) { throw new RoomIsBusyException(); } var roomCapacity = context.ClassRooms .Where(c => c.Id == eventModel.ClassRoomId) .Select(c => c.Capacity).FirstOrDefault(); if (roomCapacity < 1) { throw new RoomCapacityException(); } var events = MapService.Map(eventModel); var userEmail = ServiceHelper.GetUserEmail(eventModel.UserId); context.Events.Add(events); context.Participants.Add(new Participants { EventId = events.Id, Email = userEmail }); context.SaveChanges(); } }
public void Update(IEvent eventModel, IEvent pivotModel) { if (eventModel.ClassRoomId != pivotModel.ClassRoomId) { if (ServiceHelper.IsRoomBusy(eventModel)) { throw new RoomIsBusyException(); } } if (DateTime.Compare(eventModel.BeginingDate, pivotModel.BeginingDate) < 0) { if (ServiceHelper.IsRoomBusy(new EventModel { BeginingDate = eventModel.BeginingDate, EndingDate = pivotModel.BeginingDate, ClassRoomId = eventModel.ClassRoomId })) { throw new RoomIsBusyException(); } } if (DateTime.Compare(eventModel.EndingDate, pivotModel.EndingDate) > 0) { if (ServiceHelper.IsRoomBusy(new EventModel { BeginingDate = pivotModel.EndingDate, EndingDate = eventModel.EndingDate, ClassRoomId = eventModel.ClassRoomId })) { throw new RoomIsBusyException(); } } var events = MapService.Map(eventModel); using (var context = new ClassBookingContext()) { context.Events.Attach(events); context.Entry(events).State = EntityState.Modified; context.SaveChanges(); } }
public bool IsRoomBusy(IEvent eventModel) { return(ServiceHelper.IsRoomBusy(eventModel)); }