public async Task <ActionResult <CostCenterVM> > CreateCostCenter(CostCenterVM costcenterVM) { try { if (costcenterVM == null) { return(BadRequest()); } // Add custom model validation error CostCenter costcenter = await costcenterRepository.GetCostCenterByname(costcenterVM.CostCenter); if (costcenter != null) { ModelState.AddModelError("Name", $"CostCenter name: {costcenterVM.CostCenter.Name} already in use"); return(BadRequest(ModelState)); } await costcenterRepository.CreateCostCenter(costcenterVM); return(CreatedAtAction(nameof(GetCostCenter), new { id = costcenterVM.CostCenter.Id }, costcenterVM)); } catch (DbUpdateException Ex) { return(StatusCode(StatusCodes.Status500InternalServerError, Ex.InnerException.Message)); } }
public async Task <CostCenterVM> GetCostCenter(int id) { CostCenterVM costcenterVM = new CostCenterVM(); costcenterVM.CostCenter = await appDbContext.CostCenters.FirstOrDefaultAsync(e => e.Id == id); return(costcenterVM); }
public async Task <CostCenterVM> CreateCostCenter(CostCenterVM costcenterVM) { var result = await appDbContext.CostCenters.AddAsync(costcenterVM.CostCenter); await appDbContext.SaveChangesAsync(); costcenterVM.CostCenter = result.Entity; return(costcenterVM); }
public static CostCenter MapToModel(this CostCenterVM view) { if (view == null) { return(null); } return(new CostCenter { Id = view.Id, Name = view.Name, Description = view.Description }); }
private async Task <CostCenterVM> CheckDeserialize(HttpResponseWrapper <CostCenterVM> httpResponseWrapper) { CostCenterVM costcenterVM = new CostCenterVM(); if (httpResponseWrapper.Success) { costcenterVM = await Deserialize <CostCenterVM>(httpResponseWrapper.HttpResponseMessage, defaultJsonSerializerOptions); } else { costcenterVM.Exception = await httpResponseWrapper.GetBody(); } return(costcenterVM); }
public ActionResult Save(CostCenterVM view) { ModelState.Remove("Id"); if (ModelState.IsValid) { if (view.Id == 0) { _costCenterRepository.AddToDatabase(view.MapToModel()); } else { _costCenterRepository.UpdateInDatabase(view.MapToModel(), view.Id); } _costCenterRepository.Save(); } return(RedirectToAction("Index")); }
public async Task <ActionResult <IEnumerable <CostCenterVM> > > GetCostCentersForDropDown() { List <CostCenterVM> ListCostCenterVM = new List <CostCenterVM>(); var costCenters = await _context.CostCenters.Where(c => c.StatusTypeId == (int)EStatusType.Active).ToListAsync(); foreach (CostCenter costCenter in costCenters) { CostCenterVM costCenterVM = new CostCenterVM { Id = costCenter.Id, CostCenterCode = costCenter.CostCenterCode + " " + costCenter.CostCenterDesc, }; ListCostCenterVM.Add(costCenterVM); } return(ListCostCenterVM); }
public async Task <CostCenterVM> UpdateCostCenter(CostCenterVM costcenterVM) { CostCenter result = await appDbContext.CostCenters .FirstOrDefaultAsync(e => e.Id == costcenterVM.CostCenter.Id); if (result != null) { appDbContext.Entry(result).State = EntityState.Detached; result = mapper.Map(costcenterVM.CostCenter, result); appDbContext.Entry(result).State = EntityState.Modified; await appDbContext.SaveChangesAsync(); return(new CostCenterVM { CostCenter = result }); } return(null); }
public async Task <ActionResult <CostCenterVM> > UpdateCostCenter(int id, CostCenterVM costcenterVM) { try { if (id != costcenterVM.CostCenter.Id) { return(BadRequest("CostCenter ID mismatch")); } // Add custom model validation error CostCenter costcenter = await costcenterRepository.GetCostCenterByname(costcenterVM.CostCenter); if (costcenter != null) { ModelState.AddModelError("Name", $"CostCenter name: {costcenterVM.CostCenter.Name} already in use"); return(BadRequest(ModelState)); } var costcenterToUpdate = await costcenterRepository.GetCostCenter(id); if (costcenterToUpdate == null) { return(NotFound($"CostCenter with Id = {id} not found")); } await costcenterRepository.UpdateCostCenter(costcenterVM); return(CreatedAtAction(nameof(GetCostCenter), new { id = costcenterVM.CostCenter.Id }, costcenterVM)); } catch (DbUpdateException Ex) { return(StatusCode(StatusCodes.Status500InternalServerError, Ex.InnerException.Message)); } }
public async Task <CostCenterVM> UpdateCostCenter(int id, CostCenterVM costcenterVM) { var response = await httpService.Put($"{url}/{id}", costcenterVM); return(await CheckDeserialize(response)); }
public async Task <CostCenterVM> CreateCostCenter(CostCenterVM costcenterVM) { var response = await httpService.Post(url, costcenterVM); return(await CheckDeserialize(response)); }