Esempio n. 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);
        }
Esempio n. 2
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);
        }