public async Task <JsonResult> DeleteMasterClass([FromBody] ClassMasterDto dto) { WebApiSimpleResponse <ClassMasterDto> res; try { if (dto == null) { var ex = new Exception("Entity not found"); throw ex; } var entity = new ClassMaster { Id = dto.Id }; var doesHaveDetailClass = _classDetailRepository.GetAll().Where(x => x.ClassMasterId == dto.Id); if (doesHaveDetailClass.Count() > 0) { throw new Exception("The selected master class contains detail class(es)!"); } await _classMasterRepository.Delete(entity); res = new WebApiSimpleResponse <ClassMasterDto> { Message = ResponseMessage.OperationSucceeded.GetDescription() }; } catch (Exception ex) { Logger.ErrorException(ex.Message, ex); res = new WebApiSimpleResponse <ClassMasterDto> { ErrorFlag = true, Message = ResponseMessage.OperationFailed.GetDescription() }; } return(new JsonResult(res)); }
public JsonResult GetMasterClass(int id) { WebApiSimpleResponse <ClassMasterDto> res; try { var classMasterEntity = _classMasterRepository.GetAll().FirstOrDefault(x => x.Id == id); if (classMasterEntity == null) { var ex = new Exception("Class not found"); throw ex; } var resDto = new ClassMasterDto { Id = classMasterEntity.Id, Classname = classMasterEntity.ClassName, Location = classMasterEntity.Location, TeacherName = classMasterEntity.TeacherName, }; res = new WebApiSimpleResponse <ClassMasterDto> { Result = resDto, Message = ResponseMessage.OperationSucceeded.GetDescription() }; } catch (Exception ex) { Logger.ErrorException(ex.Message, ex); res = new WebApiSimpleResponse <ClassMasterDto> { ErrorFlag = true, Message = ResponseMessage.OperationFailed.GetDescription() }; } return(new JsonResult(res)); }
public async Task <JsonResult> UpdateMasterClass([FromBody] ClassMasterDto dto) { WebApiSimpleResponse <ClassMasterDto> res; try { if (dto == null) { var ex = new Exception("Entity not found"); throw ex; } var entity = new ClassMaster { Id = dto.Id, ClassName = dto.Classname, Location = dto.Location, TeacherName = dto.TeacherName }; await _classMasterRepository.Update(entity); res = new WebApiSimpleResponse <ClassMasterDto> { Message = ResponseMessage.OperationSucceeded.GetDescription() }; } catch (Exception ex) { Logger.ErrorException(ex.Message, ex); res = new WebApiSimpleResponse <ClassMasterDto> { ErrorFlag = true, Message = ResponseMessage.OperationFailed.GetDescription() }; } return(new JsonResult(res)); }
public async Task DeleteStudent() { var options = new DbContextOptionsBuilder <BackendTestDbContext>() .UseInMemoryDatabase(databaseName: "UpdateStudentDatabase") .Options; var savedMasterClass = new ClassMaster(); var savedDetailClass = new ClassDetail(); #region SaveMasterClass using (var context = new BackendTestDbContext(options)) { var classMasterRepository = new ClassMasterRepository(context); var classDetailRepository = new ClassDetailRepository(context); var studentRepository = new StudentRepository(context); var classDetailStudentRepository = new ClassDetailStudentRepository(context); var classController = new ClassController(classMasterRepository, classDetailRepository, studentRepository, classDetailStudentRepository); var toBeSavedEntity = new ClassMasterDto { Classname = "Computer Engineering", Location = "Some where", TeacherName = "Dr. Demberg" }; var rawJson = await classController.SaveMasterClass(toBeSavedEntity); var res = (WebApiSimpleResponse <ClassMaster>)rawJson.Value; savedMasterClass = res.Result; Assert.IsFalse(res.ErrorFlag); } #endregion #region SaveDetailClass using (var context = new BackendTestDbContext(options)) { var classMasterRepository = new ClassMasterRepository(context); var classDetailRepository = new ClassDetailRepository(context); var studentRepository = new StudentRepository(context); var classDetailStudentRepository = new ClassDetailStudentRepository(context); var classController = new ClassController(classMasterRepository, classDetailRepository, studentRepository, classDetailStudentRepository); var toBeSavedEntity = new ClassDetailDto { ClassMasterId = savedMasterClass.Id }; var rawJson = await classController.SaveDetailClass(toBeSavedEntity); var res = (WebApiSimpleResponse <ClassDetail>)rawJson.Value; savedDetailClass = res.Result; Assert.IsFalse(res.ErrorFlag); } #endregion var student = new Student(); #region SaveStudent using (var context = new BackendTestDbContext(options)) { var _studentRepository = new StudentRepository(context); var _classDetailStudentRepository = new ClassDetailStudentRepository(context); var studentController = new StudentController(_studentRepository, _classDetailStudentRepository); var toBeSavedStudent = new StudentDto { FirstName = "John", LastName = "Smith", Gpa = (decimal)15.2, Age = 20 }; var rawJsonRes1 = await studentController.SaveStudent(toBeSavedStudent); student = ((WebApiSimpleResponse <Student>)rawJsonRes1.Value).Result; Assert.IsFalse(((WebApiSimpleResponse <Student>)rawJsonRes1.Value).ErrorFlag); } #endregion #region AddStudentToDetailClass using (var context = new BackendTestDbContext(options)) { var classMasterRepository = new ClassMasterRepository(context); var classDetailRepository = new ClassDetailRepository(context); var studentRepository = new StudentRepository(context); var classDetailStudentRepository = new ClassDetailStudentRepository(context); var classController = new ClassController(classMasterRepository, classDetailRepository, studentRepository, classDetailStudentRepository); var toBeSavedEntity = new ClassDetailDto { ClassDetailId = savedDetailClass.Id, StudentDtos = new List <StudentDto> { new StudentDto { Id = student.Id } } }; var rawJson = await classController.AddStudentsToDetailClass(toBeSavedEntity); var res = (WebApiSimpleResponse <ClassDetailStudent>)rawJson.Value; Assert.IsFalse(res.ErrorFlag); } #endregion #region DeleteWithNullValue using (var context = new BackendTestDbContext(options)) { var _studentRepository = new StudentRepository(context); var _classDetailStudentRepository = new ClassDetailStudentRepository(context); var studentController = new StudentController(_studentRepository, _classDetailStudentRepository); var rawJsonDelete = await studentController.DeleteStudent(null); var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value; Assert.IsTrue(deleteStudentRes.ErrorFlag); } #endregion #region DeleteAStudentEnrolledInAClass using (var context = new BackendTestDbContext(options)) { var _studentRepository = new StudentRepository(context); var _classDetailStudentRepository = new ClassDetailStudentRepository(context); var studentController = new StudentController(_studentRepository, _classDetailStudentRepository); var toBeDeletedStudent = new StudentDto { Id = student.Id, }; var rawJsonDelete = await studentController.DeleteStudent(toBeDeletedStudent); var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value; Assert.IsTrue(deleteStudentRes.ErrorFlag); } #endregion #region RemoveStudentFromDetailClass using (var context = new BackendTestDbContext(options)) { var classMasterRepository = new ClassMasterRepository(context); var classDetailRepository = new ClassDetailRepository(context); var studentRepository = new StudentRepository(context); var classDetailStudentRepository = new ClassDetailStudentRepository(context); var classController = new ClassController(classMasterRepository, classDetailRepository, studentRepository, classDetailStudentRepository); var toBeSavedEntity = new ClassDetailDto { ClassDetailId = savedDetailClass.Id, StudentDtos = new List <StudentDto> { new StudentDto { Id = student.Id } } }; var rawJson = await classController.RemoveStudentsFromDetailClass(toBeSavedEntity); var res = (WebApiSimpleResponse <ClassMasterDto>)rawJson.Value; Assert.IsFalse(res.ErrorFlag); } #endregion #region NiceStudentDelete using (var context = new BackendTestDbContext(options)) { var _studentRepository = new StudentRepository(context); var _classDetailStudentRepository = new ClassDetailStudentRepository(context); var studentController = new StudentController(_studentRepository, _classDetailStudentRepository); var toBeDeletedStudent = new StudentDto { Id = student.Id, }; var rawJsonDelete = await studentController.DeleteStudent(toBeDeletedStudent); var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value; Assert.IsFalse(deleteStudentRes.ErrorFlag); var rawJsonGetStudentById = studentController.GetStudentById(toBeDeletedStudent.Id); var getStudentByIdRes = (WebApiSimpleResponse <StudentDto>)rawJsonGetStudentById.Value; Assert.IsTrue(getStudentByIdRes.ErrorFlag); } #endregion #region InvalidId using (var context = new BackendTestDbContext(options)) { var _studentRepository = new StudentRepository(context); var _classDetailStudentRepository = new ClassDetailStudentRepository(context); var studentController = new StudentController(_studentRepository, _classDetailStudentRepository); var toBeDeletedStudent = new StudentDto { Id = student.Id, }; var rawJsonDelete = await studentController.DeleteStudent(toBeDeletedStudent); var deleteStudentRes = (WebApiSimpleResponse <Student>)rawJsonDelete.Value; Assert.IsTrue(deleteStudentRes.ErrorFlag); } #endregion }