public async Task <IActionResult> UpdateSubject(SubjectDtoForEdit subject) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _response = await _repo.EditSubject(subject); return(Ok(_response)); }
public async Task <ServiceResponse <object> > EditSubject(SubjectDtoForEdit subject) { try { Subject checkExist = _context.Subjects.FirstOrDefault(s => s.Name.ToLower() == subject.Name.ToLower() && s.SchoolBranchId == _LoggedIn_BranchID); if (checkExist != null && checkExist.Id != subject.Id) { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.RecordAlreadyExist; return(_serviceResponse); } Subject ObjToUpdate = _context.Subjects.FirstOrDefault(s => s.Id.Equals(subject.Id)); if (ObjToUpdate != null) { ObjToUpdate.Name = subject.Name; ObjToUpdate.CreditHours = subject.CreditHours; ObjToUpdate.Active = subject.Active; _context.Subjects.Update(ObjToUpdate); 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); }