public GenericObjectResponse <List <WorkingHoursPayload> > ChangeWorkingHours(long roomId, [FromBody] List <WorkingHoursPayload> payload)
        {
            long?userId = AuthenticationService.IsAuthorized(Request, UserRole.RoomOwner);

            if (userId == null)
            {
                Response.StatusCode = 401;
                return(new GenericObjectResponse <List <WorkingHoursPayload> >(""));
            }

            RoomValidationService roomValidationService = new RoomValidationService();
            GenericStatusMessage  roomExistsValidation  = roomValidationService.ValidateRoomExistsAndOwnedByUser(roomId, userId.Value);

            if (!roomExistsValidation.Success)
            {
                Response.StatusCode = 404;
                return(new GenericObjectResponse <List <WorkingHoursPayload> >("Not found."));
            }

            WorkingHoursManipulationService service = new WorkingHoursManipulationService();
            GenericObjectResponse <List <WorkingHoursPayload> > response = service.ChangeWorkingHoursForRoom(roomId, payload);

            if (!response.Status.Success)
            {
                Response.StatusCode = 400;
            }

            return(response);
        }
        public async Task <GenericObjectResponse <RoomResponse> > AddRoom(CreateRoomPayload payload, long?userId)
        {
            try
            {
                Logger.Debug($"Attempting to create new room for user {userId}");
                RoomResponse          roomResponse     = null;
                RoomValidationService service          = new RoomValidationService();
                GenericStatusMessage  validationResult = service.ValidateRoomData(payload);
                if (!validationResult.Success)
                {
                    return(new GenericObjectResponse <RoomResponse>(validationResult.Message));
                }

                using (ReservationDataContext context = new ReservationDataContext())
                {
                    Room room = ConvertRoomFromPayload(payload, userId);
                    context.Rooms.Add(room);
                    context.SaveChanges();
                    roomResponse = ConvertRoomToResponse(room, userId);
                }

                Logger.Debug($"Attempting to create working hours for room {roomResponse.Id}");
                WorkingHoursManipulationService workingHoursService = new WorkingHoursManipulationService();
                GenericObjectResponse <List <WorkingHoursPayload> > workingHours = workingHoursService.ChangeWorkingHoursForRoom(roomResponse.Id, payload.WorkingHours);
                if (!workingHours.Status.Success)
                {
                    Logger.Error($"failed to create working hours for room {roomResponse.Id}, Deleting created room.");
                    using (ReservationDataContext context = new ReservationDataContext())
                    {
                        Room room = context.Rooms.Single(x => x.Id == roomResponse.Id);
                        context.Rooms.Remove(room);
                        context.SaveChanges();
                    }
                    return(new GenericObjectResponse <RoomResponse>($"Failed to add room due to faulty working hours: {workingHours.Status.Message}"));
                }
                roomResponse.WorkingHours = workingHours.Object;
                Logger.Debug($"Room created for user {userId}.");
                LatLon latLon = await AddLatLonForRoom(roomResponse.Id);

                if (latLon != null)
                {
                    roomResponse.Lat = latLon.Lat;
                    roomResponse.Lon = latLon.Lon;
                }
                return(new GenericObjectResponse <RoomResponse>(roomResponse));
            }
            catch (DbEntityValidationException e)
            {
                string exceptionMessage = e.EntityValidationErrors.FirstOrDefault()?.ValidationErrors.FirstOrDefault()?.ErrorMessage;
                Logger.Error($"Failed to create room. Error: '{exceptionMessage}'");
                return(new GenericObjectResponse <RoomResponse>("Failed to add the room, please contact support."));
            }
        }
        public async Task <GenericObjectResponse <RoomResponse> > AddRoom([FromBody] CreateRoomPayload payload)
        {
            long?userId = AuthenticationService.IsAuthorized(Request, UserRole.RoomOwner);

            if (userId == null)
            {
                Response.StatusCode = 401;
                return(new GenericObjectResponse <RoomResponse>(""));
            }

            RoomManipulationService service = new RoomManipulationService();
            GenericObjectResponse <RoomResponse> response = await service.AddRoom(payload, userId);

            if (!response.Status.Success)
            {
                Response.StatusCode = 400;
            }

            return(response);
        }