public async Task <Photo> Handle(Command request, CancellationToken cancellationToken) { var result = photoAccessor.CreatePhoto(request.File); var user = await userManager.FindByNameAsync(currentUserAccessor.GetCurrentUsername()); user.Photo = result; context.Images.Add(user.Photo); await context.SaveChangesAsync(cancellationToken); return(user.Photo); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var user = await userManager.FindByNameAsync(currentUserAccessor.GetCurrentUsername()); var recipe = new Recipe() { Author = user, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now, Servings = request.Servings, Description = request.Description, Id = Guid.NewGuid().ToString(), Title = request.Title, TimeOfCooking = request.TimeOfCooking, Ingredients = request.Ingredients, Cuisine = context.Cuisines.FirstOrDefault(x => x.Name == request.Cuisine), NutritionValue = request.NutritionValue }; if (Enum.TryParse(typeof(Domain.Difficulty), request.Difficulty, out object res)) { if (res != null) { recipe.Difficulty = (Domain.Difficulty)res; } } else { throw new RestException(System.Net.HttpStatusCode.BadRequest, new { message = "Wrong difficulty" }); } recipe.Categories = new List <Category>(); foreach (var cat in request.Categories) { recipe.Categories.Add(context.Categories.FirstOrDefault(x => x.Name == cat.Name)); } if (request.MainImage == null) { throw new RestException(System.Net.HttpStatusCode.BadRequest, new { message = "Main image is required" }); } recipe.MainImage = photoAccessor.CreatePhoto(request.MainImage); if (request.Images is { Count : > 0 })