Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        public void Add(IParticipant participaModel)
        {
            using (var context = new ClassBookingContext())
            {
                var duplicatedEmails = context.Participants
                                       .Count(p => p.EventId == participaModel.EventId &&
                                              p.Email == participaModel.Email);

                if (duplicatedEmails > 0)
                {
                    throw new ParticipantAlreadyRegisteredException();
                }

                var alreadyRegisteredCount = context.Participants.Count(p => p.EventId == participaModel.EventId);
                var roomId =
                    context.Events.Where(e => e.Id == participaModel.EventId).Select(e => e.ClassRoomId).FirstOrDefault();
                var roomCapacity =
                    context.ClassRooms.Where(c => c.Id == roomId).Select(c => c.Capacity).FirstOrDefault();

                if (alreadyRegisteredCount >= roomCapacity)
                {
                    throw new ParticipantCountReachedMaximumRoomCapacityException();
                }

                context.Participants.Add(new Participants
                {
                    Email   = participaModel.Email,
                    EventId = participaModel.EventId
                });
                context.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        public void ChangeRoomStatus(int id, ClassRoomStatus classRoomStatus)
        {
            using (var context = new ClassBookingContext())
            {
                var classRoom = context.ClassRooms.Find(id);
                context.ClassRooms.Attach(classRoom);
                var entry = context.Entry(classRoom);
                switch (classRoomStatus)
                {
                case ClassRoomStatus.Opened:
                    classRoom.IsLocked = false;
                    break;

                case ClassRoomStatus.Closed:
                    classRoom.IsLocked = true;

                    var eventsToDelete = context.Events.Where(e => e.ClassRoomId == id).ToList();
                    foreach (var eventToDelete in eventsToDelete)
                    {
                        context.Participants.RemoveRange(
                            context.Participants.Where(p => p.EventId == eventToDelete.Id));
                    }

                    context.Events.RemoveRange(context.Events.Where(e => e.ClassRoomId == id));

                    break;
                }
                entry.Property(e => e.IsLocked).IsModified = true;
                context.SaveChanges();
            }
        }
 public void Add(IFeedback feedbackModel)
 {
     using (var context = new ClassBookingContext())
     {
         context.Feedbacks.Add(MapService.Map(feedbackModel));
         context.SaveChanges();
     }
 }
Ejemplo n.º 5
0
 public void Add(IClassRoom classRoom)
 {
     using (var context = new ClassBookingContext())
     {
         context.ClassRooms.Add(MapService.Map(classRoom));
         context.SaveChanges();
     }
 }
Ejemplo n.º 6
0
        public void Remove(IClassRoom classRoom)
        {
            var classRooms = new ClassRooms
            {
                Id = classRoom.Id
            };

            using (var context = new ClassBookingContext())
            {
                context.ClassRooms.Attach(classRooms);
                context.ClassRooms.Remove(classRooms);
                context.SaveChanges();
            }
        }
Ejemplo n.º 7
0
        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();
            }
        }
Ejemplo n.º 8
0
        public void Remove(IEvent eventModel)
        {
            var events = new Events
            {
                Id = eventModel.Id
            };

            using (var context = new ClassBookingContext())
            {
                context.Participants.RemoveRange(context.Participants
                                                 .Where(p => p.EventId == events.Id));

                context.Events.Attach(events);
                context.Events.Remove(events);

                context.SaveChanges();
            }
        }
Ejemplo n.º 9
0
        public void Update(IClassRoom classRoom)
        {
            var updatedClassRoom = MapService.Map(classRoom);

            using (var context = new ClassBookingContext())
            {
                var old = context.ClassRooms.FirstOrDefault(c => c.Id == updatedClassRoom.Id);
                if (old != null)
                {
                    context.Entry(old).CurrentValues.SetValues(updatedClassRoom);
                    context.SaveChanges();
                }
                else
                {
                    throw new ObjectNotFoundException();
                }
            }
        }
Ejemplo n.º 10
0
        public void RemoveAllEventsFromUser(string id)
        {
            var eventsList = new List <IEvent>();
            var dateNow    = DateTime.Now;

            using (var context = new ClassBookingContext())
            {
                var events = context.Events.Where(e => e.UserId == id).ToList();
                foreach (var _event in events)
                {
                    context.Participants.RemoveRange(context.Participants
                                                     .Where(p => p.EventId == _event.Id));

                    context.Events.Attach(_event);
                    context.Events.Remove(_event);
                }
                context.SaveChanges();
            }
        }
Ejemplo n.º 11
0
        public void Remove(IParticipant paricipantModel)
        {
            var participants = new Participants();

            using (var context = new ClassBookingContext())
            {
                if (paricipantModel.Id > 0)
                {
                    participants.Id = paricipantModel.Id;
                }
                else
                {
                    participants = context.Participants.FirstOrDefault(p => p.Email == paricipantModel.Email);
                }

                context.Participants.Attach(participants);
                context.Participants.Remove(participants);

                context.SaveChanges();
            }
        }