コード例 #1
0
        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));
            }
        }
コード例 #2
0
        public async Task <CostCenterVM> GetCostCenter(int id)
        {
            CostCenterVM costcenterVM = new CostCenterVM();

            costcenterVM.CostCenter = await appDbContext.CostCenters.FirstOrDefaultAsync(e => e.Id == id);

            return(costcenterVM);
        }
コード例 #3
0
        public async Task <CostCenterVM> CreateCostCenter(CostCenterVM costcenterVM)
        {
            var result = await appDbContext.CostCenters.AddAsync(costcenterVM.CostCenter);

            await appDbContext.SaveChangesAsync();

            costcenterVM.CostCenter = result.Entity;
            return(costcenterVM);
        }
コード例 #4
0
ファイル: CostCenterMapper.cs プロジェクト: TajanaP/TRXpense
        public static CostCenter MapToModel(this CostCenterVM view)
        {
            if (view == null)
            {
                return(null);
            }

            return(new CostCenter
            {
                Id = view.Id,
                Name = view.Name,
                Description = view.Description
            });
        }
コード例 #5
0
ファイル: CostCenterService.cs プロジェクト: ahmed154/sol_ERP
        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);
        }
コード例 #6
0
        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"));
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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));
            }
        }
コード例 #10
0
ファイル: CostCenterService.cs プロジェクト: ahmed154/sol_ERP
        public async Task <CostCenterVM> UpdateCostCenter(int id, CostCenterVM costcenterVM)
        {
            var response = await httpService.Put($"{url}/{id}", costcenterVM);

            return(await CheckDeserialize(response));
        }
コード例 #11
0
ファイル: CostCenterService.cs プロジェクト: ahmed154/sol_ERP
        public async Task <CostCenterVM> CreateCostCenter(CostCenterVM costcenterVM)
        {
            var response = await httpService.Post(url, costcenterVM);

            return(await CheckDeserialize(response));
        }