public async Task <IActionResult> UpdateComponentAsync([FromBody] UpdateComponentAc updateComponentAc) { if (string.IsNullOrEmpty(updateComponentAc.Name.Trim())) { return(Ok(new SharedLookUpResponse() { HasError = true, ErrorType = SharedLookUpResponseType.Name, Message = "Component Group name can't be null or empty" })); } else if (string.IsNullOrEmpty(updateComponentAc.ShortName.Trim())) { return(Ok(new SharedLookUpResponse() { HasError = true, ErrorType = SharedLookUpResponseType.Name, Message = "Component Group short name can't be null or empty" })); } else { var instituteId = await GetUserCurrentSelectedInstituteIdAsync(); if (await iMSDbContext.BookTypes.AnyAsync(x => x.Id == updateComponentAc.Id && x.InstituteId == instituteId)) { return(Ok(await componentManagementRepository.UpdateComponentAsync(updateComponentAc, instituteId))); } else { return(Ok(new SharedLookUpResponse() { HasError = true, ErrorType = SharedLookUpResponseType.Code, Message = "Component not found" })); } } }
public async Task <SharedLookUpResponse> UpdateComponentAsync(UpdateComponentAc updateComponentAc, int instituteId) { var component = await iMSDbContext.PayrollComponents.Where(x => x.InstituteId == instituteId && x.Id != updateComponentAc.Id).ToListAsync(); var isDuplicated = component.Any(x => x.Name.ToLowerInvariant() == updateComponentAc.Name.ToLowerInvariant()); if (isDuplicated) { return new SharedLookUpResponse() { HasError = true, ErrorType = SharedLookUpResponseType.Code, Message = "Duplicate Code of Component Group, please use unique code" } } ; else { var componentUpdate = await iMSDbContext.PayrollComponents.FirstAsync(x => x.Id == updateComponentAc.Id); componentUpdate.Name = updateComponentAc.Name; componentUpdate.Description = updateComponentAc.Description; componentUpdate.Status = updateComponentAc.Status; componentUpdate.ShortName = updateComponentAc.ShortName;; componentUpdate.Others = updateComponentAc.Others; componentUpdate.IsPayslip = updateComponentAc.IsPayslip; componentUpdate.IsBasic = updateComponentAc.IsBasic; componentUpdate.GroupId = updateComponentAc.GroupId; componentUpdate.SequenceNo = updateComponentAc.SequenceNo; iMSDbContext.PayrollComponents.Update(componentUpdate); await iMSDbContext.SaveChangesAsync(); return(new SharedLookUpResponse() { HasError = false, Message = "Component Group updated successfully" }); } }