public async Task <IActionResult> UpdateSubject(TutorSubjectDtoForEdit subject) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _response = await _repo.UpdateSubject(subject); return(Ok(_response)); }
public async Task <ServiceResponse <object> > UpdateSubject(TutorSubjectDtoForEdit subject) { try { var checkExist = _context.TutorSubjects.FirstOrDefault(s => s.Name.ToLower() == subject.Name.ToLower() && s.CreatedById == _LoggedIn_UserID && s.SchoolBranchId == _LoggedIn_BranchID); if (checkExist != null && checkExist.Id != subject.Id) { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.RecordAlreadyExist; return(_serviceResponse); } var ObjToUpdate = _context.TutorSubjects.FirstOrDefault(s => s.Id.Equals(subject.Id)); if (ObjToUpdate != null) { ObjToUpdate.Name = subject.Name; ObjToUpdate.ExpertRate = subject.ExpertRate; ObjToUpdate.Active = subject.Active; _context.TutorSubjects.Update(ObjToUpdate); await _context.SaveChangesAsync(); var ObjToUpdate2 = _context.TutorProfiles.FirstOrDefault(s => s.CreatedById.Equals(_LoggedIn_UserID)); if (ObjToUpdate2 != null) { ObjToUpdate2.GradeLevels = string.Join(',', subject.GradeLevels); _context.TutorProfiles.Update(ObjToUpdate2); await _context.SaveChangesAsync(); } _serviceResponse.Message = CustomMessage.Updated; _serviceResponse.Success = true; } } catch (DbUpdateException ex) { if (ex.InnerException.Message.Contains("Cannot insert duplicate key row")) { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.SqlDuplicateRecord; } else { throw ex; } } return(_serviceResponse); }