public async Task <IActionResult> Create([FromBody] CreateTagRequest request) { var existingTag = await _tagsService.GetTagByNameAsync(request.Name); if (existingTag != null) { return(Conflict()); } var tag = new Tag { Name = request.Name, CreatorId = User.Claims.First(c => c.Type == "id").Value, CreatedOn = DateTime.UtcNow }; var created = await _tagsService.CreateAsync(tag); if (!created) { return(BadRequest()); } var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; var locationUrl = $"{baseUrl}{ApiRoutes.Tags.GetAll}"; var response = _mapper.Map <TagResponse>(tag); return(Created( locationUrl, response )); }
public async Task <IHttpActionResult> CreateAsync(TagDto data) { var result = await _tagsService.CreateAsync(data); if (result == null) { return(BadRequest()); } return(Ok(result)); }
public async Task CreateAsync_InputIsValidTag() { // Arrange var tag = new Tag { Name = "test", CreatedOn = DateTime.Now, CreatorId = "testUser" }; // Act await _tagsService.CreateAsync(tag); var tags = await _tagsService.GetAllAsync(); // Assert Assert.Single(tags); }
/// <summary> /// Aktualizuje przepis /// </summary> /// <param name="command">Obiekt edycji przepisu</param> /// <returns>Zakutalizowany przepis</returns> public async Task <Recipe> UpdateRecipeAsync(UpdateCommand command) { var current = await _dbset .Include(x => x.Images) .Include(x => x.Products) .Include("Products.Product") .Include(x => x.Tags) .FirstOrDefaultAsync(x => x.Id == command.Id); if (current == null) { throw new NullReferenceException("Przepis nie istnieje!"); } // produkty foreach (var updatedProduct in command.Products) { var dbRecipeProduct = await _db.RecipeProducts.FirstOrDefaultAsync(x => x.Product.Name == updatedProduct.Name); if (dbRecipeProduct != null) { int productId = dbRecipeProduct.ProductId; var dbProduct = await _db.Products.FindAsync(dbRecipeProduct.ProductId); if (dbProduct.Name != updatedProduct.Name) { var properProduct = await _productsService.GetOrCreateProductByNameAsync(updatedProduct.Name); productId = properProduct.Id; } _db.Entry(dbRecipeProduct).CurrentValues.SetValues(new RecipeProduct { Id = dbRecipeProduct.Id, NumberOfUnit = updatedProduct.Amount, ProductId = productId, RecipeId = dbRecipeProduct.RecipeId, UnitId = updatedProduct.Unit }); } else { var properProduct = await _productsService.GetOrCreateProductByNameAsync(updatedProduct.Name); RecipeProduct recipeProduct = new RecipeProduct { NumberOfUnit = updatedProduct.Amount, ProductId = properProduct.Id, UnitId = updatedProduct.Unit, Product = new Models.Product() { Name = updatedProduct.Name } }; current.Products.Add(recipeProduct); } } foreach (var product in current.Products.ToList()) { var detachedProduct = command.Products.FirstOrDefault(x => x.Name == product.Product.Name); if (detachedProduct == null && product.Id > 0) { current.Products.Remove(product); _db.RecipeProducts.Remove(product); } } // obrazki foreach (var updatedImage in command.Images) { var dbRecipeImage = await _db.RecipeImages.FindAsync(updatedImage.Id); if (dbRecipeImage != null) { continue; } current.Images.Add(new RecipeImage { Path = updatedImage.RelativeUrl, RecipeId = command.Id }); } foreach (var image in current.Images.ToList()) { var detachedImage = command.Images.Find(x => x.Id == image.Id); if (detachedImage == null && image.Id > 0) { current.Images.Remove(image); _db.RecipeImages.Remove(image); } } //tagi // todo: tagi jak produkty var tagsToRemove = new List <RecipeTag>(); foreach (var tag in current.Tags) { if (command.Tags.All(x => x.ToLower() != tag.Name.ToLower())) { tagsToRemove.Add(tag); } } foreach (var tag in tagsToRemove) { current.Tags.Remove(tag); } foreach (var tag in command.Tags) { bool exists = _db.RecipeTags.Any(x => x.Name.ToLower() == tag.ToLower()); if (!exists || current.Tags.All(x => x.Name.ToLower() != tag.ToLower())) { //var properTag = await _tagsService.GetOrCreateTagAsync(tag); var properTag = await _db.RecipeTags .FirstOrDefaultAsync(x => x.Name.ToLower() == tag.ToLower()); if (properTag == null) { properTag = await _tagsService.CreateAsync(new RecipeTag() { Name = tag }); } current.Tags.Add(properTag); } } //current.Tags = command.Tags.Select(x => new RecipeTag //{ // Name = x //}).ToList(); current.Name = command.Title; current.Description = command.Description; current.Difficulty = command.Difficulty; current.TimeToPrepare = command.TimeToPrepare; current.EstimatedCost = command.EstimatedCost; current.PortionCount = command.PortionCount; current.CategoryId = command.Category; //current.Images = command.Images.Select(x => new RecipeImage() //{ // Path = x.Path //}).ToList(); var updatedRecipe = await UpdateAsync(current); return(updatedRecipe); //List<RecipeProduct> recipeProducts = await _db.RecipeProducts.Where(x => x.RecipeId == command.Id).ToListAsync(); //foreach (var product in command.Products) //{ // var properProduct = await _productsService.GetOrCreateProductByNameAsync(product.Name); // RecipeProduct recipeProduct = new RecipeProduct(); // recipeProduct.NumberOfUnit = product.Amount; // recipeProduct.ProductId = properProduct.Id; // recipeProduct.UnitId = product.Unit; // //recipeProducts.Add(recipeProduct); //} //current.Products = new List<RecipeProduct>(); //foreach (var product in command.Products) //{ // if (product.Id != null) // { // var productInDb = await _db.RecipeProducts.FirstOrDefaultAsync(x => x.Id == product.Id); // if (productInDb == null) // continue; // current.Products.Add(new RecipeProduct // { // Id = (int)product.Id, // NumberOfUnit = product.Amount, // ProductId = productInDb.ProductId, // RecipeId = productInDb.RecipeId, // UnitId = product.Unit // }); // } // else // { // var properProduct = await _productsService.GetOrCreateProductByNameAsync(product.Name); // RecipeProduct recipeProduct = new RecipeProduct // { // NumberOfUnit = product.Amount, // ProductId = properProduct.Id, // UnitId = product.Unit // }; // current.Products.Add(recipeProduct); // } //} //return null; }