示例#1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var entity = JsonConvert.DeserializeObject <FormModel>(request.Model);

                if (await _context.Categories.Where(x => x.Name == entity.Name).AnyAsync())
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { CategoryName = "This category already exists" });
                }

                var catPhotoUploadResult = _photoAccessor.AddCategoryPhoto(request.File);

                var category = new Category
                {
                    Id       = Guid.NewGuid(),
                    Name     = entity.Name,
                    ImageUrl = catPhotoUploadResult.Url,
                    ImageId  = catPhotoUploadResult.PublicId,
                };

                _context.Categories.Add(category);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Problem saving changes");
            }