public async Task <IHttpActionResult> ImportRecipe(RecipeImport recipe) { try { await _recipesService.ImportRecipeAsync(recipe); } catch (Exception e) { return(InternalServerError(e)); } return(Ok("Import poprawny.")); }
/// <summary> /// Importuje przepisy na podstawie modelu do importu (crawler) /// </summary> /// <param name="command">Model importu</param> /// <returns>Przepis domenowy</returns> public async Task <Recipe> ImportRecipeAsync(RecipeImport command) { string userId = UserHelper.GetCurrentUserId(); if (String.IsNullOrEmpty(userId)) { throw new ServiceException("Nieuatoryzowana próba dodania przepisu!"); } var existing = await _dbset.FirstOrDefaultAsync(x => x.Name.ToLower() == command.Title.ToLower()); var category = await _categoriesService.ImportCategoryAsync(command.Category); if (existing != null) { return(await UpdateRecipeAsync(new UpdateCommand() { Id = existing.Id, Category = category.Id, Description = command.Description, Difficulty = command.Difficulty, EstimatedCost = command.EstimatedCost, PortionCount = command.PortionCount, Title = command.Title, TimeToPrepare = command.TimeToPrepare, Products = command.Products.Select(x => new UpdateCommand.Product() { Name = x.Name, Unit = _unitsService.GetOrCreateUnitByName(x.Unit).Id, Amount = x.Amount }).ToList(), Images = command.Images.Select(x => new UpdateCommand.Image() { Id = null, RelativeUrl = x }).ToList(), Tags = command.Tags })); } var recipe = new Recipe() { Name = command.Title, Description = command.Description, Difficulty = command.Difficulty, TimeToPrepare = command.TimeToPrepare, EstimatedCost = command.EstimatedCost, PortionCount = command.PortionCount, Images = command.Images.Select(x => new RecipeImage() { Path = x }).ToList(), Products = await _productsService.ImportProductsAsync(command.Products), CreatedDate = DateTime.Now, AuthorId = userId, CategoryId = category.Id, Tags = command.Tags.Select(x => new RecipeTag() { Name = x }).ToList(), //await _tagsService.ImportTagsAsync(command.Tags), AverageRate = command.AverageRate }; return(await CreateAsync(recipe)); }