/// <summary> /// Method to update subject - SS /// </summary> /// <param name="updateInstituteSubject">subject detail</param> /// <param name="instituteId">institute id</param> /// <returns>message</returns> public async Task <InstituteSubjectResponse> UpdateInstituteSubjectAsync(UpdateInstituteSubjectManagementAc updateInstituteSubject, int instituteId) { var subjects = await _iMSDbContext.InstituteSubjects.Where(x => x.InstituteId == instituteId && x.Id != updateInstituteSubject.Id).ToListAsync(); var isDuplicate = subjects.Any(x => x.Code.ToLowerInvariant() == updateInstituteSubject.Code.ToLowerInvariant()); if (isDuplicate) { return new InstituteSubjectResponse() { HasError = true, Message = "Duplicate subject code. Please use unique subject code", ErrorType = InstituteSubjectResponseType.Code } } ; else { var instituteSubject = await _iMSDbContext.InstituteSubjects.FirstAsync(x => x.Id == updateInstituteSubject.Id); instituteSubject.Code = updateInstituteSubject.Code; instituteSubject.IsGroup = updateInstituteSubject.IsGroup; instituteSubject.Name = updateInstituteSubject.Name; instituteSubject.Description = updateInstituteSubject.Description; _iMSDbContext.InstituteSubjects.Update(instituteSubject); await _iMSDbContext.SaveChangesAsync(); return(new InstituteSubjectResponse() { HasError = false, Message = "Subject updated successfully" }); } }
public async Task <IActionResult> UpdateInstituteSubjectAsync([FromBody] UpdateInstituteSubjectManagementAc updateInstituteSubjectManagement) { if (string.IsNullOrEmpty(updateInstituteSubjectManagement.Name.Trim())) { return(Ok(new InstituteSubjectResponse { ErrorType = InstituteSubjectResponseType.Name, HasError = true, Message = "Subject name can't be null or empty" })); } else if (string.IsNullOrEmpty(updateInstituteSubjectManagement.Code.Trim())) { return(Ok(new InstituteSubjectResponse { ErrorType = InstituteSubjectResponseType.Code, HasError = true, Message = "Subject code name can't be null or empty" })); } else { var loggedInUserInstituteId = await GetUserCurrentSelectedInstituteIdAsync(); if (await _iMSDbContext.InstituteSubjects.AnyAsync(x => x.Id == updateInstituteSubjectManagement.Id && x.InstituteId == loggedInUserInstituteId)) { return(Ok(await _instituteSubjectManagementRepository.UpdateInstituteSubjectAsync(updateInstituteSubjectManagement, loggedInUserInstituteId))); } else { return(Ok(new InstituteSubjectResponse { ErrorType = InstituteSubjectResponseType.Other, HasError = true, Message = "Subject not found" })); } } }