Exemplo n.º 1
0
        public async Task <ActionResult <ServiceResponse <Booking> > > AddBooking([FromBody] BookingEvent toAddEvent)
        {
            var response = new ServiceResponse <Booking>();

            Int32.TryParse(toAddEvent.Resource, out int roomId);
            DateTime.TryParse(toAddEvent.Start, out DateTime fromDate);
            DateTime.TryParse(toAddEvent.End, out DateTime toDate);
            var guest = (await _service.GetGuestByEmail(toAddEvent.Text));

            if (guest == null)
            {
                try
                {
                    guest = await _service.AddGuest(toAddEvent.Text, "");
                }
                catch (System.Exception e)
                {
                    return(StatusCode(500, e.Message));
                }
            }

            try
            {
                var booking = new Booking
                {
                    RoomId   = roomId,
                    GuestId  = guest.Id,
                    FromDate = fromDate,
                    ToDate   = toDate
                };
                var dbBooking = await _service.AddBooking(booking);

                response.Data = booking;
                return(CreatedAtAction(nameof(GetBookingById), new { id = booking.Id }, response));
            }
            catch (Exception e)
            {
                response.Message = e.Message;
                return(BadRequest(response));
            }
        }