public ActionResult Create(PriceCategoryModel priceCategory) { try { if (ModelState.IsValid) { priceCategory.ActionUserName = User.Identity.Name; priceCategory.ActionType = "Create"; priceCategory.ActionTime = DateTime.Now; var modelDTO = toDTOMapper.Map <PriceCategoryModel, PriceCategoryDTO>(priceCategory); if (!priceCategoryService.Check(modelDTO)) { priceCategoryService.Create(modelDTO); return(RedirectToAction("Index")); } ModelState.AddModelError("", "This category of price already exists"); return(View()); } else { ModelState.AddModelError("", "Something went wrong"); return(View()); } } catch (Exception ex) { return(HttpNotFound()); } }
public ActionResult Edit(PriceCategoryModel model) { try { if (ModelState.IsValid) { model.ActionUserName = User.Identity.Name; model.ActionType = "Edit"; model.ActionTime = DateTime.Now; model.PriceCategoryID = Int32.Parse(Request.Url.Segments[3]); var modelDTO = toDTOMapper.Map <PriceCategoryModel, PriceCategoryDTO>(model); priceCategoryService.Update(modelDTO.PriceCategoryID, modelDTO); return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Something went wrong"); return(View()); } } catch (Exception ex) { return(HttpNotFound()); } }
public void PriceCategoryPostTest() { var categoryMapper = new MapperConfiguration( cfg => cfg.CreateMap <Category, CategoryModel>()).CreateMapper(); PriceCategoryModel priceCategoryModel = new PriceCategoryModel() { PriceCategoryID = 3, Price = 350, StartDate = DateTime.Parse("01.06.2021"), EndDate = DateTime.Parse("30.08.2021"), CategoryName = categoryMapper.Map <Category, CategoryModel>(DataForTests.Categories[2]) }; EFWorkUnitMock.Setup(x => x.PriceCategories.Search(DataForTests.PriceCategories[0])).Returns(true); var toDTO = new MapperConfiguration( cfg => { cfg.CreateMap <PriceCategory, PriceCategoryDTO>().ReverseMap(); cfg.CreateMap <Category, CategoryDTO>().ReverseMap(); }).CreateMapper(); var priceCategoryDTO = toDTO.Map <PriceCategory, PriceCategoryDTO>(DataForTests.PriceCategories[0]); PriceCategoryServiceMock.Setup(a => a.Check(priceCategoryDTO)).Returns(true); PriceCategoryController controller = new PriceCategoryController(PriceCategoryServiceMock.Object); var httpResponse = controller.Post(httpRequest, priceCategoryModel); var result = httpResponse.StatusCode; Assert.AreEqual(HttpStatusCode.OK, result); }
public void PriceCategoryGetTest() { int id = 2; var priceCategory = DataForTests.PriceCategories[id - 1]; var toDTO = new MapperConfiguration( cfg => { cfg.CreateMap <PriceCategory, PriceCategoryDTO>().ReverseMap(); cfg.CreateMap <Category, CategoryDTO>().ReverseMap(); }).CreateMapper(); var priceCategoryDTO = toDTO.Map <PriceCategory, PriceCategoryDTO>(priceCategory); EFWorkUnitMock.Setup(x => x.PriceCategories.Get(id)).Returns(priceCategory); PriceCategoryServiceMock.Setup(a => a.Get(id)).Returns(priceCategoryDTO); var priceCategoryService = new PriceCategoryService(EFWorkUnitMock.Object); PriceCategoryController controller = new PriceCategoryController(PriceCategoryServiceMock.Object); var httpResponse = controller.Get(httpRequest, id); var result = httpResponse.Content.ReadAsAsync <PriceCategoryModel>(); PriceCategoryModel expected = mapper.Map <PriceCategoryDTO, PriceCategoryModel>(priceCategoryService.Get(id)); Assert.AreEqual(expected, result.Result); }
public HttpResponseMessage Get(HttpRequestMessage request, int id) { try { PriceCategoryDTO priceCategory = service.Get(id); var data = new PriceCategoryModel(); if (priceCategory != null) { data = mapper.Map <PriceCategoryDTO, PriceCategoryModel>(priceCategory); return(request.CreateResponse(HttpStatusCode.OK, data)); } return(request.CreateResponse(HttpStatusCode.NotFound)); } catch (Exception ex) { return(request.CreateResponse(HttpStatusCode.NotFound)); } }
// POST: api/PriceCategory public HttpResponseMessage Post(HttpRequestMessage request, [FromBody] PriceCategoryModel value) { try { var mapper = new MapperConfiguration( cfg => { cfg.CreateMap <PriceCategoryModel, PriceCategoryDTO>().ReverseMap(); cfg.CreateMap <CategoryModel, CategoryDTO>().ReverseMap(); }).CreateMapper(); var data = mapper.Map <PriceCategoryModel, PriceCategoryDTO>(value); if (!service.Check(data)) { service.Create(data); return(request.CreateResponse(HttpStatusCode.OK)); } return(request.CreateResponse(HttpStatusCode.Conflict)); } catch (Exception ex) { return(request.CreateResponse(HttpStatusCode.BadRequest)); } }
// PUT: api/PriceCategory/5 public HttpResponseMessage Put(HttpRequestMessage request, int id, [FromBody] PriceCategoryModel value) { try { var priceCategory = service.Get(id); if (priceCategory != null) { var mapper = new MapperConfiguration( cfg => { cfg.CreateMap <PriceCategoryModel, PriceCategoryDTO>().ReverseMap(); cfg.CreateMap <CategoryModel, CategoryDTO>().ReverseMap(); }).CreateMapper(); var data = mapper.Map <PriceCategoryModel, PriceCategoryDTO>(value); service.Update(id, data); return(request.CreateResponse(HttpStatusCode.OK)); } return(request.CreateResponse(HttpStatusCode.NotFound)); } catch (Exception ex) { return(request.CreateResponse(HttpStatusCode.BadRequest)); } }