コード例 #1
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));
        }
コード例 #2
0
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var location = await _locationsRepository.GetAsync(id);

            if (location == null)
            {
                return(NotFound());
            }

            return(View(location));
        }
コード例 #3
0
        public async Task <ActionResult <LocationDto> > Get(int locationId)
        {
            var locationFromRepo = await _locationsRepo.GetAsync(locationId);

            if (locationFromRepo is null)
            {
                return(NotFound());
            }

            var locationDto = _mapper.Map <LocationDto>(locationFromRepo);

            return(Ok(locationDto));
        }
コード例 #4
0
 public async Task <Locations> GetLocationAsync(int locationId)
 {
     return(await _locationsRepository.GetAsync(locationId));
 }