コード例 #1
0
        public Schedule Put(int id, [FromBody] Schedule schedule)
        {
            if (!ModelState.IsValid)
            {
                throw new Exception();
            }

            Schedule scheduleDb = _scheduleRepository.GetSingle(id);

            if (scheduleDb == null)
            {
                throw new KeyNotFoundException();
            }
            else
            {
                scheduleDb.Title       = schedule.Title;
                scheduleDb.Location    = schedule.Location;
                scheduleDb.Description = schedule.Description;
                scheduleDb.Status      = schedule.Status;
                scheduleDb.Type        = schedule.Type;
                scheduleDb.TimeStart   = schedule.TimeStart;
                scheduleDb.TimeEnd     = schedule.TimeEnd;

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

                foreach (var user in schedule.Attendees)
                {
                    scheduleDb.Attendees.Add(user);
                }

                _scheduleRepository.Commit();
            }
            return(scheduleDb);
        }
コード例 #2
0
        public IActionResult Put(int id, [FromBody] ScheduleViewModel schedule)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var scheduleDb = _scheduleRepository.GetSingle(id);

            if (scheduleDb == null)
            {
                return(NotFound());
            }
            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();

            Mapper.Map <Schedule, ScheduleViewModel>(scheduleDb);

            return(new NoContentResult());
        }
コード例 #3
0
        public async Task UpdateAsync(int id, string title, string description, DateTime timeStart, DateTime timeEnd,
                                      string location, int creatorId, string type, string status)
        {
            var schedule = await _scheduleRepository.GetAsync(id);

            if (schedule == null)
            {
                throw new ServiceException(ErrorCodes.ScheduleNotFound,
                                           $"Schedule with this id: {id} not exists.");
            }

            schedule.SetTitle(title);
            schedule.SetDescription(description);
            schedule.SetTimeStart(timeStart);
            schedule.SetTimeEnd(timeEnd);
            schedule.SetLocation(location);
            schedule.SetCreator(creatorId);
            schedule.Type   = type;
            schedule.Status = status;

            _scheduleRepository.Update(schedule);
            _attendeeRepository.DeleteWhere(a => a.ScheduleId == id);

            foreach (var attendee in schedule.Attendees)
            {
                await _attendeeRepository.AddAsync(new Attendee(id, attendee.Id));
            }
            await _unitOfWork.SaveChangesAsync();
        }
コード例 #4
0
        public IActionResult Delete(int id)
        {
            User _userDb = _userRepository.GetSingle(id);

            if (_userDb == null)
            {
                return(new NotFoundResult());
            }
            else
            {
                IEnumerable <Attendee> _attendees = _attendeeRepository.FindBy(a => a.UserId == id);
                IEnumerable <Schedule> _schedules = _scheduleRepository.FindBy(s => s.CreatorId == id);

                foreach (var attendee in _attendees)
                {
                    _attendeeRepository.Delete(attendee);
                }

                foreach (var schedule in _schedules)
                {
                    _attendeeRepository.DeleteWhere(a => a.ScheduleId == schedule.Id);
                    _scheduleRepository.Delete(schedule);
                }

                _userRepository.Delete(_userDb);

                _userRepository.Commit();

                return(new NoContentResult());
            }
        }
コード例 #5
0
        public async Task RemoveUserSchedulesAsync(int id)
        {
            var schedules = await _scheduleRepository.FindByAsync(s => s.CreatorId == id);

            foreach (var schedule in schedules)
            {
                _attendeeRepository.DeleteWhere(a => a.ScheduleId == schedule.Id);
                _scheduleRepository.Delete(schedule);
            }
        }
コード例 #6
0
        public Task <Unit> Handle(ScheduleDeleteAttendeeCommand request, CancellationToken cancellationToken)
        {
            if (_attendeeRepository.FindBy(a => a.UserId == request.UserId &&
                                           a.ScheduleId == request.ScheduleId) == null)
            {
                throw new KeyNotFoundException($"User {request.UserId} is not associated with schedule {request.ScheduleId}");
            }
            _attendeeRepository.DeleteWhere(a => a.UserId == request.UserId && a.ScheduleId == request.ScheduleId);
            _attendeeRepository.Commit();

            return(Unit.Task);
        }
コード例 #7
0
        public Task <ScheduleViewModel> Handle(ScheduleUpdateCommand request, CancellationToken cancellationToken)
        {
            var schedule   = request.ScheduleViewModel;
            var scheduleDb = _scheduleRepository.GetSingle(request.Id);

            if (scheduleDb != null)
            {
                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 == request.Id);

                foreach (var userId in schedule.Attendees)
                {
                    scheduleDb.Attendees.Add(new Attendee {
                        ScheduleId = request.Id, UserId = userId
                    });
                }

                try
                {
                    _scheduleRepository.Update(scheduleDb);
                    _scheduleRepository.Commit();

                    return(Task.FromResult(_mapper.Map <Schedule, ScheduleViewModel>(scheduleDb)));
                }

                catch (Exception e)
                {
                    throw new Exception($"Can't update schedule {request.Id}", e);
                }
            }

            throw new KeyNotFoundException($"Schedule {request.Id} not found");
        }
コード例 #8
0
        public Task <Unit> Handle(ScheduleDeleteCommand request, CancellationToken cancellationToken)
        {
            var schedule = _scheduleRepository.GetSingle(request.Id);

            if (schedule == null)
            {
                throw new KeyNotFoundException($"Schedule {request.Id} not found");
            }

            try
            {
                _attendeeRepository.DeleteWhere(a => a.ScheduleId == request.Id);
                _scheduleRepository.Delete(schedule);
                _scheduleRepository.Commit();
            }

            catch (Exception e)
            {
                throw new Exception($"Cannot delete schedule {request.Id}", e);
            }

            return(Unit.Task);
        }
コード例 #9
0
        public Task <Unit> Handle(UserDeleteCommand request, CancellationToken cancellationToken)
        {
            var user = _userRepository.GetSingle(request.Id);

            if (user == null)
            {
                throw new KeyNotFoundException($"User {request.Id} not found");
            }

            try
            {
                _attendeeRepository.DeleteWhere(a => a.UserId == request.Id);

                _userRepository.Delete(user);
                _userRepository.Commit();
            }

            catch (Exception e)
            {
                throw new Exception($"Cannot delete user {request.Id}", e);
            }

            return(Unit.Task);
        }