public async Task <ActionResult <FiscalYearVM> > CreateFiscalYear(FiscalYearVM fiscalyearVM) { try { if (fiscalyearVM == null) { return(BadRequest()); } // Add custom model validation error FiscalYear fiscalyear = await fiscalyearRepository.GetFiscalYearByname(fiscalyearVM.FiscalYear); if (fiscalyear != null) { ModelState.AddModelError("Name", $"FiscalYear name: {fiscalyearVM.FiscalYear.Name} already in use"); return(BadRequest(ModelState)); } await fiscalyearRepository.CreateFiscalYear(fiscalyearVM); return(CreatedAtAction(nameof(GetFiscalYear), new { id = fiscalyearVM.FiscalYear.Id }, fiscalyearVM)); } catch (DbUpdateException Ex) { return(StatusCode(StatusCodes.Status500InternalServerError, Ex.InnerException.Message)); } }
public async Task <FiscalYearVM> GetFiscalYear(int id) { FiscalYearVM fiscalyearVM = new FiscalYearVM(); fiscalyearVM.FiscalYear = await appDbContext.FiscalYears.FirstOrDefaultAsync(e => e.Id == id); return(fiscalyearVM); }
public async Task <FiscalYearVM> CreateFiscalYear(FiscalYearVM fiscalyearVM) { var result = await appDbContext.FiscalYears.AddAsync(fiscalyearVM.FiscalYear); await appDbContext.SaveChangesAsync(); fiscalyearVM.FiscalYear = result.Entity; return(fiscalyearVM); }
private async Task <FiscalYearVM> CheckDeserialize(HttpResponseWrapper <FiscalYearVM> httpResponseWrapper) { FiscalYearVM fiscalyearVM = new FiscalYearVM(); if (httpResponseWrapper.Success) { fiscalyearVM = await Deserialize <FiscalYearVM>(httpResponseWrapper.HttpResponseMessage, defaultJsonSerializerOptions); } else { fiscalyearVM.Exception = await httpResponseWrapper.GetBody(); } return(fiscalyearVM); }
public async Task <FiscalYearVM> UpdateFiscalYear(FiscalYearVM fiscalyearVM) { FiscalYear result = await appDbContext.FiscalYears .FirstOrDefaultAsync(e => e.Id == fiscalyearVM.FiscalYear.Id); if (result != null) { appDbContext.Entry(result).State = EntityState.Detached; result = mapper.Map(fiscalyearVM.FiscalYear, result); appDbContext.Entry(result).State = EntityState.Modified; await appDbContext.SaveChangesAsync(); return(new FiscalYearVM { FiscalYear = result }); } return(null); }
public async Task <ActionResult <FiscalYearVM> > UpdateFiscalYear(int id, FiscalYearVM fiscalyearVM) { try { if (id != fiscalyearVM.FiscalYear.Id) { return(BadRequest("FiscalYear ID mismatch")); } // Add custom model validation error FiscalYear fiscalyear = await fiscalyearRepository.GetFiscalYearByname(fiscalyearVM.FiscalYear); if (fiscalyear != null) { ModelState.AddModelError("Name", $"FiscalYear name: {fiscalyearVM.FiscalYear.Name} already in use"); return(BadRequest(ModelState)); } var fiscalyearToUpdate = await fiscalyearRepository.GetFiscalYear(id); if (fiscalyearToUpdate == null) { return(NotFound($"FiscalYear with Id = {id} not found")); } await fiscalyearRepository.UpdateFiscalYear(fiscalyearVM); return(CreatedAtAction(nameof(GetFiscalYear), new { id = fiscalyearVM.FiscalYear.Id }, fiscalyearVM)); } catch (DbUpdateException Ex) { return(StatusCode(StatusCodes.Status500InternalServerError, Ex.InnerException.Message)); } }
public async Task <FiscalYearVM> UpdateFiscalYear(int id, FiscalYearVM fiscalyearVM) { var response = await httpService.Put($"{url}/{id}", fiscalyearVM); return(await CheckDeserialize(response)); }
public async Task <FiscalYearVM> CreateFiscalYear(FiscalYearVM fiscalyearVM) { var response = await httpService.Post(url, fiscalyearVM); return(await CheckDeserialize(response)); }