public void Create(CreateCategoryServiceModel model) { var category = new Category() { Name = model.Name, Description = model.Description }; data.Categories.Add(category); data.SaveChanges(); }
//-------------- Methods ---------------- public void Create(CreateCategoryServiceModel model) { Category category = new Category() { Name = model.Name, Description = model.Description, }; this.context.Categories.Add(category); this.context.SaveChanges(); }
public IActionResult Create(CreateCategoryInputModel model) { if (!this.ModelState.IsValid) { return(this.RedirectToAction("Error", "Home")); } var categoryServiceModel = new CreateCategoryServiceModel() { Name = model.Name, Description = model.Description }; this.categoryService.Create(categoryServiceModel); return(this.RedirectToAction("All", "Categories")); }
public async Task <CategoryServiceModel> CreateAsync(CreateCategoryServiceModel model) { var category = new Category { Parentid = model.ParentId, IsLeaf = true }; var parentCategory = await this.context.Categories.FirstOrDefaultAsync(x => x.Id == model.ParentId && x.IsActive); category.Level = parentCategory.Level + 1; this.context.Categories.Add(category.FillCommonProperties()); var categoryTranslation = new CategoryTranslation { CategoryId = category.Id, Name = model.Name, Language = model.Language }; this.context.CategoryTranslations.Add(categoryTranslation.FillCommonProperties()); foreach (var file in model.Files.OrEmptyIfNull()) { var categoryImage = new CategoryImage { CategoryId = category.Id, MediaId = file, }; this.context.CategoryImages.Add(categoryImage.FillCommonProperties()); } await this.context.SaveChangesAsync(); return(await this.GetAsync(new GetCategoryServiceModel { Id = category.Id, Language = model.Language, OrganisationId = model.OrganisationId, Username = model.Username })); }
public async Task <IActionResult> Save(CategoryRequestModel request) { var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim); if (request.Id.HasValue) { var serviceModel = new UpdateCategoryServiceModel { Id = request.Id, Files = request.Files, Name = request.Name, ParentId = request.ParentCategoryId, Language = CultureInfo.CurrentCulture.Name, Username = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value, OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value) }; var validator = new UpdateCategoryModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var category = await this.categoryService.UpdateAsync(serviceModel); if (category != null) { var response = new CategoryResponseModel { Id = category.Id, IsLeaf = category.IsLeaf, Level = category.Level, Name = category.Name, Order = category.Order, ParentCategoryName = category.ParentCategoryName, ParentId = category.ParentId, ThumbnailMediaId = category.ThumbnailMediaId, LastModifiedDate = category.LastModifiedDate, CreatedDate = category.CreatedDate }; return(this.StatusCode((int)HttpStatusCode.OK, response)); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); } else { var serviceModel = new CreateCategoryServiceModel { Name = request.Name, ParentId = request.ParentCategoryId, Files = request.Files, Language = CultureInfo.CurrentCulture.Name, Username = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value, OrganisationId = GuidHelper.ParseNullable(sellerClaim?.Value) }; var validator = new CreateCategoryModelValidator(); var validationResult = await validator.ValidateAsync(serviceModel); if (validationResult.IsValid) { var category = await this.categoryService.CreateAsync(serviceModel); if (category != null) { var response = new CategoryResponseModel { Id = category.Id, IsLeaf = category.IsLeaf, Level = category.Level, Name = category.Name, Order = category.Order, ParentCategoryName = category.ParentCategoryName, ParentId = category.ParentId, ThumbnailMediaId = category.ThumbnailMediaId, LastModifiedDate = category.LastModifiedDate, CreatedDate = category.CreatedDate }; return(this.StatusCode((int)HttpStatusCode.Created, response)); } } throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity); } }