예제 #1
0
        public async Task <IActionResult> CreateAccommodationAsync([FromRoute] int days, [FromRoute] int userId, [FromBody] Accommodation accommodation)
        {
            try
            {
                var owner = await _userService.GetUserAsync(userId);

                if (!CheckIfUserIsAllowedToPerformAction(owner, GetIdOfLoggedInUser(HttpContext), GetUsertypeOfLoggedInUser(HttpContext)))
                {
                    return(BadRequest("Restricted acces, this action can only be done by administrator or the user that will be the owner of the accommodation!"));
                }

                var newAccommodation = await _accommodationService.CreateAccommodationAsync(owner, accommodation.Address, accommodation.SquareMeters, accommodation.AmountOfBedrooms, accommodation.KilometersFromCenter, accommodation.Description, accommodation.PricePerNight, accommodation.CancellationDeadlineInDays, days);

                return(CreatedAtAction(nameof(GetAccommodationAsync), new { id = newAccommodation.Id }, newAccommodation));
            }
            catch (Exception ex)
            {
                if (ex.GetType().IsAssignableFrom(typeof(NotFoundException)))
                {
                    return(NotFound(ex.Message));
                }
                else
                {
                    return(BadRequest(ex.Message));
                }
            }
        }