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."));
            }
        }
 private Room ConvertRoomFromPayload(CreateRoomPayload payload, long?userId)
 {
     return(new Room
     {
         OwnerId = userId,
         Name = payload.Name,
         Size = payload.Size ?? 0,
         MaxNumberOfPeople = payload.MaxNumberOfPeople ?? 0,
         Country = payload.Country,
         City = payload.City,
         Street = payload.Street,
         BuildingNumber = payload.BuildingNumber,
         IsActive = payload.IsActive
     });
 }
        public GenericStatusMessage ValidateRoomData(CreateRoomPayload payload, bool required = true)
        {
            ValidationHelper            helper      = new ValidationHelper();
            List <GenericStatusMessage> validations = new List <GenericStatusMessage>
            {
                helper.ValidateStringValue(payload.Name, "Name", 2, 100, required),
                helper.ValidateValueThanZero(payload.Size, "Size", true, required),
                helper.ValidateValueThanZero(payload.MaxNumberOfPeople, "Maximum number of people", true, required),
                helper.ValidateStringValue(payload.Country, "Country", 2, 20, required),
                helper.ValidateStringValue(payload.City, "City", 2, 20, required),
                helper.ValidateStringValue(payload.Street, "Street", 2, 50, required),
                helper.ValidateValueThanZero(payload.BuildingNumber, "Building number", true, required)
            };

            return(validations.FirstOrDefault(x => !x.Success) ?? new GenericStatusMessage(true));
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        public override void Up()
        {
            UserManipulationService userManipulationService = new UserManipulationService();
            CreateUserPayload       owner = new CreateUserPayload
            {
                FirstName       = "Ron",
                LastName        = "Shachar",
                Country         = "Israel",
                City            = "Tel Aviv",
                Street          = "Rambam",
                BuildingNumber  = 6,
                Password        = "******",
                ConfirmPassword = "******",
                Username        = "******"
            };

            userManipulationService.AddUser(owner, UserRole.RoomOwner);
            long ownerId = 0;

            using (ReservationDataContext context = new ReservationDataContext())
            {
                ownerId = context.Users.Single(x => x.Username == "*****@*****.**").Id;
            }

            RoomManipulationService roomManipulationService = new RoomManipulationService();
            CreateRoomPayload       roomPayload             = new CreateRoomPayload
            {
                Name           = "Globo Gym",
                Country        = "Israel",
                City           = "Givatayim",
                Street         = "Borochov",
                BuildingNumber = 5,
                IsActive       = true,
                WorkingHours   = CreateDefaultWorkingHours(Days.Sunday, Days.Tuesday, Days.Thursday)
            };

            roomManipulationService.AddRoom(roomPayload, ownerId);
        }
Ejemplo n.º 6
0
        public async Task <GenericObjectResponse <RoomResponse> > ChangeRoomData([FromBody] CreateRoomPayload payload, long roomId)
        {
            long?userId = AuthenticationService.IsAuthorized(Request, UserRole.RoomOwner);

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

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

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

            RoomManipulationService roomManipulationService = new RoomManipulationService();

            return(await roomManipulationService.ChangeRoomData(payload, roomId));
        }
        public async Task <GenericObjectResponse <RoomResponse> > ChangeRoomData(CreateRoomPayload payload, long roomId)
        {
            RoomValidationService service            = new RoomValidationService();
            GenericStatusMessage  validationResponse = service.ValidateRoomData(payload, false);
            RoomResponse          roomResponse       = null;

            if (validationResponse.Success)
            {
                using (ReservationDataContext context = new ReservationDataContext())
                {
                    Room room = context.Rooms.Include(x => x.WorkingHours).Single(x => x.Id == roomId);
                    room.Name = payload.Name ?? room.Name;
                    room.Size = payload.Size ?? room.Size;
                    room.MaxNumberOfPeople = payload.MaxNumberOfPeople ?? room.MaxNumberOfPeople;
                    room.Country           = payload.Country ?? room.Country;
                    room.City           = payload.City ?? room.City;
                    room.Street         = payload.Street ?? room.Street;
                    room.BuildingNumber = payload.BuildingNumber ?? room.BuildingNumber;
                    context.SaveChanges();
                    roomResponse = ConvertRoomToResponse(room, room.OwnerId);
                }

                LatLon latLon = await AddLatLonForRoom(roomResponse.Id);

                if (latLon != null)
                {
                    roomResponse.Lat = latLon.Lat;
                    roomResponse.Lon = latLon.Lon;
                }

                return(new GenericObjectResponse <RoomResponse>(roomResponse));
            }
            else
            {
                return(new GenericObjectResponse <RoomResponse>(validationResponse.Message));
            }
        }