Exemple #1
0
        public async Task <Unit> Handle(DeleteRoomTypeCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.RoomTypes.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(RoomType), request.Id);
            }

            var hasRooms = context.Rooms.FirstOrDefault(od => od.RoomTypeId == entity.Id);

            if (hasRooms != null)
            {
                throw new DeleteFailureException(nameof(RoomType), "There are existing rooms associated with this room type.");
            }

            if (!string.IsNullOrEmpty(entity.Image))
            {
                try
                {
                    await imageWriter.RemoveImage(entity.Image);
                }
                catch (Exception)
                {
                    throw new UploadImageException(entity.Image, typeof(IImageWriter));
                }
            }

            context.RoomTypes.Remove(entity);

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #2
0
        public async Task <Unit> Handle(DeleteRoomCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.Rooms.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Room), request.Id);
            }

            context.Rooms.Remove(entity);

            await context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemple #3
0
        public async Task <int> Handle(CreateBookingCommand request, CancellationToken cancellationToken)
        {
            var user = await context.Users.FindAsync(request.Id);

            if (user is null)
            {
                throw new NotFoundException(nameof(User), request.Id);
            }

            var room = context.Rooms.Include(r => r.RoomType).FirstOrDefault(r => r.Id == request.RoomId);

            if (room is null)
            {
                throw new NotFoundException(nameof(Room), request.RoomId);
            }

            var rooms = mediator.Send(new GetAvailableRoomsQuery()
            {
                Code = room.RoomType.Code, In = request.DateFrom, Out = request.DateTo
            });

            if (!rooms.Result.Any(f => f.Id == request.RoomId))
            {
                throw new NotFoundException(nameof(Room), request.RoomId);
            }

            var entity = new Booking
            {
                DateFrom  = request.DateFrom,
                DateTo    = request.DateTo,
                RoomId    = request.RoomId,
                GuestId   = user.UserType is UserType.Guest ? request.Id : (int?)null,
                ManagerId = user.UserType is UserType.Manager ? request.Id : (int?)null,
            };

            try
            {
                context.Bookings.Add(entity);
            }
            catch (Exception) { }

            await context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
Exemple #4
0
        public async Task <UserViewModel> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.Users.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(User), request.Id);
            }

            entity.Email          = request.Email;
            entity.FirstName      = request.FirstName;
            entity.LastName       = request.LastName;
            entity.AdditionalInfo = request.AdditionalInfo;

            await context.SaveChangesAsync(cancellationToken);

            return(mapper.Map <UserViewModel>(entity));
        }
Exemple #5
0
        public async Task <RoomTypeViewModel> Handle(CreateRoomTypeCommand request, CancellationToken cancellationToken)
        {
            var entity = new RoomType
            {
                Code       = request.Code,
                Cost       = request.Cost,
                Categories = request.Categories,
                Facilities = request.Facilities,
                Size       = request.Size,
                BedType    = request.BedType
            };

            context.RoomTypes.Add(entity);

            await context.SaveChangesAsync(cancellationToken);

            return(mapper.Map <RoomTypeViewModel>(entity));
        }
Exemple #6
0
        public async Task <RoomViewModel> Handle(UpdateRoomCommand request, CancellationToken cancellationToken)
        {
            var parent = context.RoomTypes.FirstOrDefault(e => e.Code == request.RoomType);

            var entity = await context.Rooms.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Room), request.Id);
            }

            entity.RoomType = parent ?? throw new NotFoundException(nameof(RoomType), request.RoomType);
            entity.Number   = request.Number;
            entity.Floor    = request.Floor;

            await context.SaveChangesAsync(cancellationToken);

            return(mapper.Map <RoomViewModel>(entity));
        }
Exemple #7
0
        public async Task <RoomTypeViewModel> Handle(UpdateRoomTypeCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.RoomTypes.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(RoomType), request.Id);
            }

            entity.Code       = request.Code;
            entity.Cost       = request.Cost;
            entity.Categories = request.Categories;
            entity.Facilities = request.Facilities;
            entity.Size       = request.Size;
            entity.BedType    = request.BedType;

            await context.SaveChangesAsync(cancellationToken);

            return(mapper.Map <RoomTypeViewModel>(entity));
        }
Exemple #8
0
        public async Task <string> Handle(UploadRoomTypeImageCommand request, CancellationToken cancellationToken)
        {
            var entity = await context.RoomTypes.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(RoomType), request.Id);
            }

            string image = entity.Image;

            if (!string.IsNullOrEmpty(image))
            {
                try
                {
                    await imageWriter.RemoveImage(image);
                }
                catch (Exception)
                {
                    throw new UploadImageException(request.ImageFile.FileName, typeof(IImageWriter));
                }
            }


            try
            {
                image = await imageWriter.UploadImage(request.ImageFile);
            }
            catch (Exception)
            {
                throw new UploadImageException(request.ImageFile.FileName, typeof(IImageWriter));
            }

            entity.Image = image;

            await context.SaveChangesAsync(cancellationToken);

            return(image);
        }
Exemple #9
0
        public async Task <RoomViewModel> Handle(CreateRoomCommand request, CancellationToken cancellationToken)
        {
            var parent = context.RoomTypes.FirstOrDefault(f => f.Code == request.RoomType);

            if (parent == null)
            {
                throw new NotFoundException(nameof(RoomType), request.RoomType);
            }

            var entity = new Room
            {
                RoomTypeId = parent.Id,
                Floor      = request.Floor,
                Number     = request.Number
            };

            context.Rooms.Add(entity);

            await context.SaveChangesAsync(cancellationToken);

            return(mapper.Map <RoomViewModel>(entity));
        }
Exemple #10
0
        public async Task <UserViewModel> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var user = await service.LoadUserFromAuthServerAsync(request.Token);

            if (user.UserType == UserType.Guest)
            {
                user = new Guest(user)
                {
                    RegisterDate = dateTime.Now
                }
            }
            ;
            else
            {
                user = new Manager(user);
            }

            context.Users.Add(user);

            await context.SaveChangesAsync(cancellationToken);

            return(mapper.Map <UserViewModel>(user));
        }