public async Task <IActionResult> GetCatalog([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var catalog = await catalogRepository.FindById(id); if (catalog == null) { return(NotFound()); } CatalogDTO dto = new CatalogDTO(); dto.CatalogId = catalog.CatalogId; dto.Date = catalog.Date; dto.CatalogDescription = catalog.CatalogDescription; dto.CatalogName = catalog.CatalogName; dto.products = new List <ProductDTO>(); foreach (CatalogProduct cp in catalog.CatalogProducts) { ProductDTO pdto = productToDTO(cp.Product); dto.products.Add(pdto); } return(Ok(dto)); }
public async Task <IActionResult> PostCatalog([FromBody] CatalogDTO catalogDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var catalog = await catalogRepository.Add(catalogDTO); if (catalog == null) { return(BadRequest()); } CatalogDTO dto = new CatalogDTO(); dto.CatalogId = catalog.CatalogId; dto.Date = catalog.Date; dto.CatalogDescription = catalog.CatalogDescription; dto.CatalogName = catalog.CatalogName; dto.products = new List <ProductDTO>(); foreach (CatalogProduct cp in catalog.CatalogProducts) { ProductDTO pdto = productToDTO(cp.Product); dto.products.Add(pdto); } return(CreatedAtAction("PostCatalog", dto)); }
private void buttonCreateCatalog_Click(object sender, EventArgs e) { string catalogName = textBoxCatalogName.Text; string catalogDetail = textBoxCatalogDetail.Text; CatalogDTO catalog = new CatalogDTO(); catalog.CatalogDetails = catalogDetail; catalog.CatalogName = catalogName; service.createCatalog(catalog); loadCatalog(); }
public static Specification toSpecification(CatalogDTO catalogDTO, Specification specification) { specification.cpu = catalogDTO.cpu; specification.ram = catalogDTO.ram; specification.screen = catalogDTO.screen; specification.os = catalogDTO.os; specification.createdAt = catalogDTO.id > 0 ? specification.createdAt : DateTime.Now; specification.updatedAt = DateTime.Now; specification.catalogid = catalogDTO.id; return(specification); }
private void buttonEditCatalog_Click(object sender, EventArgs e) { int catalogId = Convert.ToInt32(textBoxIdCatalog.Text); string catalogName = textBoxCatalogName.Text; string catalogDetail = textBoxCatalogDetail.Text; CatalogDTO catalog = new CatalogDTO(); catalog.Id = catalogId; catalog.CatalogDetails = catalogDetail; catalog.CatalogName = catalogName; service.editCatalog(catalog); loadCatalog(); }
public ActionResult Update(int id) { Catalog catalog = catalogService.GetByID(id); CatalogDTO catalogDTO = new CatalogDTO() { Name = catalog.Name, Explanation = catalog.Explanation, ImageUrl = catalog.ImageUrl, ID = catalog.ID }; return(View(catalogDTO)); }
public async Task TestPostCatalogFail() { CatalogDTO dto = new CatalogDTO(); dto.Date = "06/01/2019"; dto.CatalogDescription = "This is a mock Catalog to test the post"; dto.CatalogName = "Test_Catalog"; dto.products = new List <ProductDTO>(); //Should not create the new catalog if the name already exists var result = await controller.PostCatalog(dto); Assert.IsType <BadRequestResult>(result); }
public CatalogDTO GetCatalog() { var result = new CatalogDTO() { Cycles = _context.Cycles.Where(e => e.DeactivateDatetime == null).ToList(), Levels = _context.Levels.Where(e => e.DeactivateDatetime == null).ToList(), PaymentTypes = _context.PaymentTypes.Where(e => e.DeactivateDatetime == null).ToList(), ActivityTypes = _context.ActivityTypes.Where(e => e.DeactivateDatetime == null).ToList(), Cities = _context.Cantons.Where(e => e.DeactivateDatetime == null).ToList(), Distritos = _context.Distritos.Where(e => e.DeactivateDatetime == null).ToList(), Provinces = _context.StateOrProvinces.Where(e => e.DeactivateDatetime == null).ToList() }; return(result); }
public void AddСatalog(CatalogDTO сatalogDTO) { if (сatalogDTO.Name == null) { throw new ValidationException("Название не установлено", ""); } Catalog menu = new Catalog() { Info = сatalogDTO.Info, Name = сatalogDTO.Name, ProviderId = сatalogDTO.ProviderId }; Database.Catalog.Create(menu); Database.Save(); }
public ActionResult Update(CatalogDTO catalogDTO, HttpPostedFileBase image) { Catalog catalog = catalogService.GetByID(catalogDTO.ID); catalog.Explanation = catalogDTO.Explanation; catalog.Name = catalogDTO.Name; if (image == null) { catalogService.Update(catalog); } else { catalog.ImageUrl = new byte[image.ContentLength]; image.InputStream.Read(catalog.ImageUrl, 0, image.ContentLength); catalogService.Update(catalog); } catalogService.Update(catalog); return(Redirect("/SysAdmin/Catalog/List")); }
public void EditСatalog(CatalogDTO сatalogDTO) { if (сatalogDTO.Name == null) { throw new ValidationException("Название не установлено", ""); } Catalog сatalog = Database.Catalog.Get(сatalogDTO.Id); if (сatalog == null) { throw new ValidationException("Каталог не найдено", ""); } сatalog.Info = сatalogDTO.Info; сatalog.Name = сatalogDTO.Name; Database.Catalog.Update(сatalog); Database.Save(); }
public async Task TestDeleteProductSuccess() { //Should update Catalog by deleting a product from its list var result = await controller.AddProduct(1, 1); result = await controller.DeleteProduct(1, 1); Assert.IsType <OkObjectResult>(result); OkObjectResult new_res = (OkObjectResult)await controller.GetCatalog(1); CatalogDTO new_dto = (CatalogDTO)new_res.Value; //Information inside the retrived CatalogDTO should be updated Assert.Equal("Test_Catalog", new_dto.CatalogName); Assert.Equal("This is a mock Catalog", new_dto.CatalogDescription); Assert.Equal("06/01/2019", new_dto.Date); Assert.True(1 == new_dto.CatalogId); Assert.True(0 == new_dto.products.Count); }
public static Catalog toCatalog(CatalogDTO catalogDTO, Catalog catalog) { catalog.name = catalogDTO.name; catalog.pictureuri = catalogDTO.pictureuri; catalog.price = catalogDTO.price; catalog.quantity = catalogDTO.quantity; catalog.description = catalogDTO.description; catalog.content = catalogDTO.content; if (catalogDTO.id == 0) { catalog.createdAt = DateTime.Now; catalog.status = true; } else { catalog.createdAt = catalog.createdAt; } catalog.updatedAt = DateTime.Now; catalog.catalogbrandid = catalogDTO.catalogbrandid; catalog.catalogtypeid = catalogDTO.catalogtypeid; return(catalog); }
public IEnumerable <CatalogDTO> GetCatalog() { List <CatalogDTO> dtos = new List <CatalogDTO>(); foreach (Catalog catalog in catalogRepository.FindAll()) { CatalogDTO dto = new CatalogDTO(); dto.CatalogId = catalog.CatalogId; dto.Date = catalog.Date; dto.CatalogDescription = catalog.CatalogDescription; dto.CatalogName = catalog.CatalogName; dto.products = new List <ProductDTO>(); foreach (CatalogProduct cp in catalog.CatalogProducts) { ProductDTO pdto = productToDTO(cp.Product); dto.products.Add(pdto); } dtos.Add(dto); } return(dtos); }
public async Task TestDeleteCatalogSuccess() { //Should remove the catalog var result = await controller.DeleteCatalog(2); Assert.IsType <OkObjectResult>(result); OkObjectResult new_res = (OkObjectResult)result; CatalogDTO new_dto = (CatalogDTO)new_res.Value; //Information inside the elimanated CatalogDTO should be equivalent to the requested catalog for deletion Assert.Equal("Second_Test_Catalog", new_dto.CatalogName); Assert.Equal("This is another mock Catalog", new_dto.CatalogDescription); Assert.Equal("06/01/2019", new_dto.Date); Assert.True(2 == new_dto.CatalogId); //Should not be possible to get the catalog once its deleted var result2 = await controller.GetCatalog(2); Assert.IsType <NotFoundResult>(result2); }
public async Task <IActionResult> DeleteCatalog([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var catalog = await catalogRepository.Remove(id); if (catalog == null) { return(NotFound()); } CatalogDTO dto = new CatalogDTO(); dto.CatalogId = catalog.CatalogId; dto.Date = catalog.Date; dto.CatalogDescription = catalog.CatalogDescription; dto.CatalogName = catalog.CatalogName; dto.products = new List <ProductDTO>(); return(Ok(dto)); }
public CatalogDTO GetСatalog(int?id) { if (id == null) { throw new ValidationException("Не установлено id каталога", ""); } var сatalog = Database.Catalog.Get(id.Value); if (сatalog == null) { throw new ValidationException("Каталог не найдено", ""); } CatalogDTO сatalogDTO = new CatalogDTO() { Id = сatalog.Id, Info = сatalog.Info, Name = сatalog.Name, ProviderId = сatalog.ProviderId }; return(сatalogDTO); }
public async Task <IActionResult> Update(int id, CatalogDTO CatalogDTO) { if (CatalogDTO.CatalogId != id) { return(BadRequest()); } try { var Catalog = _mapper.Map <CatalogDTO, Catalog>(CatalogDTO); await _unitOfWork.Catalogs.Update(id, Catalog); } catch (DbUpdateConcurrencyException) { if (!await CatalogExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task TestPostCatalogSuccess() { CatalogDTO dto = new CatalogDTO(); dto.Date = "06/01/2019"; dto.CatalogDescription = "This is a mock Catalog to test the post"; dto.CatalogName = "Post_Test_Catalog"; dto.products = new List <ProductDTO>(); //Should create the new catalog var result = await controller.PostCatalog(dto); Assert.IsType <CreatedAtActionResult>(result); OkObjectResult new_res = (OkObjectResult)await controller.GetCatalog(3); CatalogDTO new_dto = (CatalogDTO)new_res.Value; //Information inside the retrived CatalogDTO should be equivalent to the newly created catalog Assert.Equal("Post_Test_Catalog", new_dto.CatalogName); Assert.Equal("This is a mock Catalog to test the post", new_dto.CatalogDescription); Assert.Equal("06/01/2019", new_dto.Date); Assert.True(3 == new_dto.CatalogId); }
public void createCatalog(CatalogDTO catalog) { catalogService.create(catalog); }
public void edit(CatalogDTO catalog) { catalogRepository.edit(mapper.Map <CatalogDTO, Catalog>(catalog)); }
private bool SaveCatalog(int id, DbContextTransaction transaction, CatalogDTO catalogDTO) { if (id == 0) { var count = _context.Catalogs.Count(x => x.name == catalogDTO.name); if (count > 0) { return(false); } var catalog = new Catalog(); catalog = CatalogMapper.toCatalog(catalogDTO, catalog); try { _context.Catalogs.Add(catalog); _context.SaveChanges(); } catch (Exception) { transaction.Rollback(); return(false); } catalogDTO.id = _context.Catalogs.SingleOrDefault(x => x.name == catalogDTO.name).id; var history = new History() { catalogid = catalogDTO.id, unit = catalogDTO.quantity, inputprice = catalogDTO.price, createdAt = DateTime.Now, updatedAt = DateTime.Now }; var specification = new Specification(); specification = CatalogMapper.toSpecification(catalogDTO, specification); try { _context.Histories.Add(history); _context.Specifications.Add(specification); _context.SaveChanges(); return(true); } catch (Exception) { transaction.Rollback(); return(false); } } else { var catalog = _context.Catalogs.Find(catalogDTO.id); var history = _context.Histories.FirstOrDefault(x => x.catalogid == catalog.id); if (history != null) { if (catalogDTO.quantity > catalog.quantity) { int unit = catalogDTO.quantity - catalog.quantity; var newHistory = new History() { catalogid = history.catalogid, inputprice = history.inputprice, unit = unit, createdAt = DateTime.Now, updatedAt = DateTime.Now }; _context.Histories.Add(newHistory); } } else { if (catalogDTO.quantity > catalog.quantity) { int unit = catalogDTO.quantity - catalog.quantity; var newHistory = new History() { catalogid = catalog.id, inputprice = catalog.price - Convert.ToDecimal(0.05 * Convert.ToDouble(catalog.price)), unit = unit, createdAt = DateTime.Now, updatedAt = DateTime.Now }; _context.Histories.Add(newHistory); } } var specification = _context.Specifications.SingleOrDefault(x => x.catalogid == catalogDTO.id); catalog = CatalogMapper.toCatalog(catalogDTO, catalog); specification = CatalogMapper.toSpecification(catalogDTO, specification); try { _context.SaveChanges(); return(true); } catch (Exception) { transaction.Rollback(); return(false); } } }
// GET public IActionResult Index(CatalogDTO catalogDto) { var categories = _dbContext.Categories .Select(c => new CategoryDTO(c.Id, c.Name)) .ToList(); var query = _dbContext.Products.Select(p => p); if (catalogDto.CategoryId != null) { query = query.Where(p => p.Category.Id == catalogDto.CategoryId.Value); } if (!string.IsNullOrWhiteSpace(catalogDto.Query)) { query = query.Where(p => p.Name.ToLower().Contains(catalogDto.Query.ToLower())); } query = query.Where(p => p.Price >= catalogDto.PriceMin && p.Price <= catalogDto.PriceMax); query = query.Where(p => p.Rating >= catalogDto.RatingMin && p.Rating <= catalogDto.RatingMax); if (catalogDto.Filters != null) { foreach (var filter in catalogDto.Filters) { query = query.Where(filter.Expression); } } if (catalogDto.SortingOption == 0) { query = query.OrderByDescending(p => p.Rating).ThenBy(p => p.Name); } else { query = query.OrderBy(p => p.Price).ThenBy(p => p.Name); } int count = query.Count(); query = query.Skip(catalogDto.Page * _productsPerPage).Take(_productsPerPage); var products = query.Select(p => new ProductCardDTO() { Id = p.Id, Price = p.Price, Name = p.Name, ImagePath = p.Image.ImagePath, Rating = p.Rating }).ToList(); CategoryDTO category = null; if (catalogDto.CategoryId != null) { category = categories.FirstOrDefault(c => c.Id == catalogDto.CategoryId); } var model = new CatalogViewModel() { Categories = categories, Category = category, ProductList = products, Page = catalogDto.Page, NumberOfPages = ((count - 1) / _productsPerPage) + 1, Query = catalogDto.Query, PriceMin = catalogDto.PriceMin, PriceMax = catalogDto.PriceMax, RatingMin = catalogDto.RatingMin, RatingMax = catalogDto.RatingMax, ProductsCount = count }; if (catalogDto.CategoryId != null) { var properties = _dbContext.Categories.ById(catalogDto.CategoryId.Value) .SelectMany(cat => cat.Properties).Include(p => p.Type) .ToList(); if (catalogDto.Filters != null && catalogDto.Filters.Count > 0) { var filterDtoMap = catalogDto.Filters .ToDictionary(dto => dto.PropertyId, dto => dto); model.Filters = properties .Select(p => _filterViewModelProvider.GetFilterViewModel(p, filterDtoMap[p.Id])) .ToList(); } else { model.Filters = properties .Select(p => _filterViewModelProvider.GetFilterViewModel(p)) .ToList(); } } return(View("Catalog", model)); }
public void create(CatalogDTO catalog) { catalogRepository.create(mapper.Map <CatalogDTO, Catalog>(catalog)); }
public void editCatalog(CatalogDTO catalog) { catalogService.edit(catalog); }