public void PostApartment_WithValidDto_ReturnsCreatedResponse()
        {
            // Setup
            InitializeTest();
            _locationsRepository.AddAsync(new Location
            {
                Id         = 1,
                Building   = "X",
                Flat       = "x",
                StreetName = "X"
            });

            var createAptDto = new CreateApartmentDto
            {
                NumberOfBeds = 1,
                RentalPrice  = 9,
                SquareMeters = 10,
                LocationId   = 1
            };

            //Act
            var response = _controller.Post(createAptDto);

            //Assert
            response.Result.Should().BeOfType <CreatedAtActionResult>("valid CreateApartmentDto provided");
        }
예제 #2
0
        public async Task <ActionResult <ApartmentDto> > Post(CreateApartmentDto createApartmentDto)
        {
            var location = await _locationsRepo.GetAsync(createApartmentDto.LocationId);

            if (location is null)
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, "The location with the specified Id does not exist."));
            }

            var existingApartmentForLocation = await _apartmentsRepo.GetByLocationIdAsync(createApartmentDto.LocationId);

            if (existingApartmentForLocation != null)
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, "The apartment in this location already been added."));
            }

            var apartment = _mapper.Map <Apartment>(createApartmentDto);
            await _apartmentsRepo.AddAsync(apartment);

            apartment.Location = location;

            var apartmentDto = _mapper.Map <ApartmentDto>(apartment);

            return(CreatedAtAction("Get", new { apartmentId = apartment.Id }, apartmentDto));
        }
        public void PostApartment_WithNotExistingLocation_ReturnsNotFound()
        {
            // Setup
            InitializeTest();

            var createAptDto = new CreateApartmentDto
            {
                NumberOfBeds = 1,
                RentalPrice  = 9,
                SquareMeters = 10,
                LocationId   = 111
            };

            //Act
            var response = _controller.Post(createAptDto);

            //Assert
            response.Result.Should().BeOfType <ObjectResult>("non existing locationId provided")
            .Which.StatusCode.Value.Equals(422);
        }