Beispiel #1
0
        public IActionResult Create(ScheduleViewModel scheduleVm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Schedule.Model.Schedule _schedule = Mapper.Map <ScheduleViewModel, Schedule.Model.Schedule>(scheduleVm);
            _schedule.DateCreated = DateTime.Now;

            _scheduleRepository.Add(_schedule);
            _scheduleRepository.Commit();

            foreach (var userId in scheduleVm.Attendees)
            {
                _schedule.Attendees.Add(new Attendee {
                    UserId = userId
                });
            }
            _scheduleRepository.Commit();

            scheduleVm = Mapper.Map <Schedule.Model.Schedule, ScheduleViewModel>(_schedule);

            CreatedAtRouteResult result = CreatedAtRoute("GetSchedule", new { controller = "Schedules", id = scheduleVm.Id }, scheduleVm);

            return(result);
        }
Beispiel #2
0
        public IActionResult Delete(int id, int attendeeId)
        {
            Schedule.Model.Schedule _schedule =
                _scheduleRepository.GetSingle(id);

            if (_schedule == null)
            {
                return(NotFound());
            }

            _attendeeRepository.DeleteWhere(a => a.ScheduleId == id && a.UserId == attendeeId);
            _attendeeRepository.Commit();

            return(new ContentResult());
        }
Beispiel #3
0
        public IActionResult Delete(int id)
        {
            Schedule.Model.Schedule _schedule =
                _scheduleRepository.GetSingle(id);

            if (_schedule == null)
            {
                return(NotFound());
            }
            //remove all of the foreign key links
            _attendeeRepository.DeleteWhere(x => x.ScheduleId == _schedule.Id);
            _scheduleRepository.Delete(_schedule);

            _scheduleRepository.Commit();

            return(new ContentResult());
        }
Beispiel #4
0
        public IActionResult Put(int id, [FromBody] ScheduleViewModel schedule)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Schedule.Model.Schedule _scheduleDb = _scheduleRepository.GetSingle(id);

            if (_scheduleDb == null)
            {
                return(NotFound());
            }
            else
            {
                _scheduleDb.Title       = schedule.Title;
                _scheduleDb.Location    = schedule.Location;
                _scheduleDb.Description = schedule.Description;
                _scheduleDb.Status      = (ScheduleStatus)Enum.Parse(typeof(ScheduleStatus), schedule.Status);
                _scheduleDb.Type        = (ScheduleType)Enum.Parse(typeof(ScheduleType), schedule.Type);
                _scheduleDb.TimeStart   = schedule.TimeStart;
                _scheduleDb.TimeEnd     = schedule.TimeEnd;

                // Remove current attendees
                _attendeeRepository.DeleteWhere(a => a.ScheduleId == id);

                foreach (var userId in schedule.Attendees)
                {
                    _scheduleDb.Attendees.Add(new Attendee {
                        ScheduleId = id, UserId = userId
                    });
                }

                _scheduleRepository.Commit();
            }

            schedule = Mapper.Map <Schedule.Model.Schedule, ScheduleViewModel>(_scheduleDb);

            return(new NoContentResult());
        }