public async Task <IActionResult> PostReservation([FromBody] ReservationForModificationsDto reservationForModificationsDto) { var invokingUserId = int.Parse(User.FindFirst(claim => claim.Type == ClaimTypes.NameIdentifier).Value); //trying to update an existing reservation if (reservationForModificationsDto.Id != 0) { var res = await _repository.GetReservationAsync(reservationForModificationsDto.Id); if (res == null) { return(BadRequest("The reservation you're trying to update doesn't exist")); } if (res.CreatedByUserId != invokingUserId) { return(BadRequest("You're not the owner of this reservation, so you can't modify it!")); } } var reservation = await _repository.CreateOrUpdateReservationAsync(invokingUserId, reservationForModificationsDto); var reservationDto = _mapper.Map <Reservation, ReservationDto>(reservation, opt => opt.AfterMap((source, target) => { target.YouLikeIt = _repository.YouLikeReservationAsync(invokingUserId, target.Id); })); return(Ok(reservationDto)); }
public async Task <Reservation> CreateOrUpdateReservationAsync(int invokingUserId, ReservationForModificationsDto reservationForModificationsDto) { var contact = await this.CreateOrUpdateContactByNameAsync(invokingUserId, _mapper.Map <ReservationForModificationsDto, ContactForModificationsDto>(reservationForModificationsDto)); var toAdd = reservationForModificationsDto.Id == 0; Reservation reservation; if (toAdd) { reservation = new Reservation { ContactId = contact.Id, CreatedByUserId = invokingUserId, CreatedDateTime = DateTime.Now } } ; else { reservation = await this.GetReservationAsync(reservationForModificationsDto.Id); reservation.ContactId = contact.Id; reservation.CreatedDateTime = DateTime.Now; } if (toAdd) { await _reservationDbContext.Reservations.AddAsync(reservation); } await _unitOfWork.CompleteAsync(); return(await GetReservationAsync(reservation.Id)); }