コード例 #1
0
        public async Task <IActionResult> UpdateBooking(int bookingId, [FromBody] BookingToCreateDto bookingForUpdateDto)
        {
            if (bookingForUpdateDto.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repository.GetUser(bookingForUpdateDto.UserId);

            if (userFromRepo.UserRole != UserRole.Admin)
            {
                return(Unauthorized());
            }
            if (!ValidateBookingExists(bookingForUpdateDto.BookingType))
            {
                return(BadRequest("Booking type does not exist"));
            }
            var bookingFromRepo = await _repository.GetBooking(bookingId);

            var bookingToUpdate = _mapper.Map <BookingToCreateDto, BookingSubject>(bookingForUpdateDto, bookingFromRepo);

            _repository.Update(bookingToUpdate);
            if (await _repository.SaveAll())
            {
                return(NoContent());
            }
            return(BadRequest("Error occurred while updating booking"));
        }
コード例 #2
0
        public async Task <IActionResult> CreateBooking(int userId, [FromBody] BookingToCreateDto bookingForCreation)
        {
            if (bookingForCreation.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repository.GetUser(bookingForCreation.UserId);

            if (userFromRepo.UserRole != UserRole.Admin)
            {
                return(Unauthorized());
            }
            if (!ValidateBookingExists(bookingForCreation.BookingType))
            {
                return(BadRequest("Booking type does not exist"));
            }

            var bookingToCreate = _mapper.Map <BookingToCreateDto, BookingSubject>(bookingForCreation);

            userFromRepo.BookingSubjects.Add(bookingToCreate);
            if (await _repository.SaveAll())
            {
                var bookingToReturn = _mapper.Map <BookingToCreateDto>(bookingToCreate);
                return(CreatedAtRoute("GetBooking", new { id = bookingToReturn.Id }, bookingToReturn));
            }
            return(BadRequest("Error in creating Booking"));
        }