Beispiel #1
0
        public async Task <IEnumerable <ValidationResult> > ValidateCreateAsync(CalendarRequestModel model, int ownerId)
        {
            var errors = new List <ValidationResult>();

            if (await _calendarRepository.AnyAsync(c => c.Name.Equals(model.Name) && c.OwnerId == ownerId))
            {
                errors.AddError($"You already have a calendar with name {model.Name}.", "Name");
            }

            return(errors);
        }
Beispiel #2
0
        public async Task <IActionResult> Create([FromBody] CalendarRequestModel model)
        {
            (await _calendarValidator.ValidateCreateAsync(model, this.GetUserId())).ToModelState(ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.ToStringEnumerable()));
            }

            var domainCalendar = _mapper.Map <Calendar>(model);
            var id             = await _calendarService.CreateCalendarAsync(domainCalendar, this.GetUserId());

            return(Ok(new { Id = id }));
        }
Beispiel #3
0
        public async Task <IActionResult> Update(int id, [FromBody] CalendarRequestModel model)
        {
            var userId = this.GetUserId();

            // TODO: Replace for custom exception.

            if (!await _calendarService.CanEditAsync(id, userId))
            {
                throw new ArgumentException();
            }

            var calendar = await _calendarService.GetCalendarByIdAsync(id, nameof(Calendar.Owner));

            // Owner can't have any calendars with the same names.
            // If calendar is edited by another user that can permissions, we need to validate it too.
            (await _calendarValidator.ValidateUpdateAsync(id, model, calendar.Owner.Id)).ToModelState(ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.ToStringEnumerable()));
            }

            var oldName = calendar.Name.Clone() as string;

            // TODO: Use AutoMapper instead.
            calendar.Id          = id;
            calendar.Name        = model.Name;
            calendar.Description = model.Description;
            calendar.Color       = model.Color;

            await _calendarService.UpdateCalendarAsync(calendar);

            if (userId != calendar.Owner.Id)
            {
                await _calendarManager.CalendarEditedAsync(calendar.Owner.Id, User.Identity.Name, _mapper.Map <CalendarViewModel>(calendar), oldName);
            }

            return(Ok());
        }