Exemplo n.º 1
0
        public async Task <IActionResult> ExpandScheduleAsync([FromRoute] int id, [FromBody] int days)
        {
            try
            {
                var acc = await _accommodationService.GetAccommodationAsync(id);

                if (!CorrectUserOrAdmin(acc.Owner.Id, GetIdOfLoggedInUser(HttpContext), GetUsertypeOfLoggedInUser(HttpContext)))
                {
                    return(BadRequest("Restricted acces, this action can only be done by administrator or owner of the accommodation!"));
                }

                await _accommodationService.ExpandScheduleOfAccommodationWithXAmountOfDaysAsync(id, days);

                return(NoContent());
            }
            catch (Exception ex)
            {
                if (ex.GetType().IsAssignableFrom(typeof(NotFoundException)))
                {
                    return(NotFound(ex.Message));
                }
                else
                {
                    return(BadRequest(ex.Message));
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> BookAsync([FromBody] string[] bookingInfo)
        {
            try
            {
                var accommodation = await _accommodationService.GetAccommodationAsync(int.Parse(bookingInfo[3]));

                var booker = await _userService.GetUserAsync(int.Parse(bookingInfo[1]));

                if (!CheckIfUserIsAllowedToPerformAction(booker, GetIdOfLoggedInUser(HttpContext), GetUsertypeOfLoggedInUser(HttpContext)))
                {
                    return(BadRequest($"Must be performed by a customer with ID {booker.Id}, or by admin on behalf of a customer with ID {booker.Id}!"));
                }

                var booking = await _bookingService.BookAsync(bookingInfo[0], booker, int.Parse(bookingInfo[2]), accommodation);

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