예제 #1
0
        public void Execute(CreateHotelDto dto)
        {
            this.editHotelValidation.ValidateAndThrow(dto);

            var hotel = this.context.Hotels
                        .Include(h => h.Location)
                        .Include(h => h.Amenities)
                        .FirstOrDefault(h => h.Id == dto.Id);

            if (hotel == null)
            {
                throw new EntityNotFoundException(dto.Id);
            }
            ;

            hotel.Amenities.Where(amenities => amenities.HotelId == hotel.Id)
            .ToList()
            .ForEach(amenity => hotel.Amenities.Remove(amenity));

            foreach (var amenity in dto.Amenities)
            {
                hotel.Amenities.Add(new HotelAmenity
                {
                    AmenityId = amenity.AmenityId,
                    HotelId   = hotel.Id
                });
            }

            dto.Location.Id = hotel.LocationId;

            this.mapper.Map(dto, hotel);
            this.mapper.Map(dto.Location, hotel.Location);

            this.context.SaveChanges();
        }
예제 #2
0
        public IActionResult Put(int id,
                                 [FromBody] CreateHotelDto dto,
                                 [FromServices] IEditHotelCommand editHotelCommand)
        {
            dto.Id          = id;
            dto.Location.Id = id;

            _dispatcher.DispatchCommand(editHotelCommand, dto);
            return(NoContent());
        }
예제 #3
0
        public void Execute(CreateHotelDto hotelDto)
        {
            this.createHotelValidation.ValidateAndThrow(hotelDto);

            var hotel = this.mapper.Map <Hotel>(hotelDto);

            this.context.Hotels.Add(hotel);

            this.context.SaveChanges();
        }
예제 #4
0
        public async Task <IActionResult> CreateHotel([FromBody] CreateHotelDto hotelDto)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError($"Invalid POST attempt in {nameof(CreateHotel)}");
                return(BadRequest(ModelState));
            }
            var hotel = _mapper.Map <Hotel>(hotelDto);
            await _unitOfWork.Hotels.Insert(hotel);

            await _unitOfWork.Save();

            return(CreatedAtRoute("GetHotel", new { id = hotel.Id }, hotel));
        }
예제 #5
0
 public IActionResult Post([FromBody] CreateHotelDto createHotelDto,
                           [FromServices] ICreateHotelCommand createHotelCommand)
 {
     _dispatcher.DispatchCommand(createHotelCommand, createHotelDto);
     return(StatusCode(StatusCodes.Status201Created));
 }