public IActionResult CreateClassEntity( [FromBody] ClassEntityForCreationDto newClassEntityDto) { if (newClassEntityDto == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (_repo.ClassEntityExists(newClassEntityDto.OwnerIdentityName)) { return(BadRequest("You already have a class.")); } var finalClassEntity = _mapper.Map <Entities.ClassEntity>(newClassEntityDto); _repo.AddClassEntity(finalClassEntity); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a class for user {newClassEntityDto.OwnerIdentityName}.")); } var createdClassEntityToReturn = _mapper.Map <ClassEntityDto>(finalClassEntity); return(CreatedAtAction("GetClassEntityByOwnerIdentityName", new { ownerIdentityName = createdClassEntityToReturn.OwnerIdentityName }, createdClassEntityToReturn)); }
public IActionResult CreateReplaceProgressRecord( [FromBody] ProgressRecordForCreationDto newProgressRecord) { if (newProgressRecord == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (!_repo.StudentExists(newProgressRecord.StudentId) || !_repo.WeekExists(newProgressRecord.WeekId) || !_repo.TrackedItemExists(newProgressRecord.TrackedItemId)) { return(NotFound()); } var finalTrackedItem = _mapper.Map <Entities.ProgressRecord>(newProgressRecord); //START: DUPLICATE ITEM CHECK var existingProgressRecord = _repo.GetProgressRecordByForeignKeys( finalTrackedItem.ClassEntityId, finalTrackedItem.StudentId, finalTrackedItem.TrackedItemId, finalTrackedItem.WeekId); if (existingProgressRecord != null) { _repo.DeleteProgressRecord(existingProgressRecord); //return UnprocessableEntity("There is already a Progress Record with those Id's"); } //END: DUPLICATE ITEM CHECK _repo.AddProgressRecord(finalTrackedItem); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a progress record with these Foreign Keys:" + $"\nClassEntityId:{newProgressRecord.ClassEntityId}" + $"\nStudentId:{newProgressRecord.StudentId}" + $"\nTrackedItemId:{newProgressRecord.TrackedItemId}" + $"\nWeekId:{newProgressRecord.WeekId}")); } var progressRecordToReturn = _mapper.Map <ProgressRecordDto>(finalTrackedItem); return(CreatedAtAction("GetProgressRecord", new { progressRecordId = progressRecordToReturn.Id }, progressRecordToReturn)); }
public IActionResult CreateWeek( string ownerIdentityName, [FromBody] WeekForCreationDto newWeekDto) { if (newWeekDto == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (!_repo.ClassEntityExists(ownerIdentityName)) { return(NotFound()); } var finalWeek = _mapper.Map <Entities.Week>(newWeekDto); //START: Might be able to replace this by defining student name as a Key value for EFCore var weeks = _repo.GetAllWeeksFromClass(ownerIdentityName); foreach (var s in weeks) { if (s.Number == finalWeek.Number) { return(UnprocessableEntity("There is already a Week with that number. How did that happen?")); } } //STOP: Might be able to replace this by defining student name as a Key value for EFCore _repo.AddWeek(ownerIdentityName, finalWeek); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a week with number: {newWeekDto.Number}.")); } var weekToReturn = _mapper.Map <WeekDto>(finalWeek); return(CreatedAtAction("GetWeek", new { weekId = weekToReturn.Id }, weekToReturn)); }
public IActionResult CreateStudent( string ownerIdentityName, [FromBody] StudentForCreationDto newStudentDto) { if (newStudentDto == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (!_repo.ClassEntityExists(ownerIdentityName)) { return(NotFound()); } var finalStudent = _mapper.Map <Entities.Student>(newStudentDto); //START: Might be able to replace this by defining student name as a Key value for EFCore var classStudents = _repo.GetAllStudentsFromClass(ownerIdentityName); foreach (var s in classStudents) { if (s.Name == finalStudent.Name) { return(UnprocessableEntity("There is already a Student with that name in this Class")); } } //STOP: Might be able to replace this by defining student name as a Key value for EFCore _repo.AddStudent(ownerIdentityName, finalStudent); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a student with name: {newStudentDto.Name}.")); } var createdStudentToReturn = _mapper.Map <StudentDto>(finalStudent); return(CreatedAtAction("GetStudent", new { studentId = createdStudentToReturn.Id }, createdStudentToReturn)); }
public IActionResult CreateTrackedItem( string ownerIdentityName, [FromBody] TrackedItemForCreationDto newTrackedItemDto) { if (newTrackedItemDto == null) { return(BadRequest()); } if (ModelState.IsValid == false) { return(BadRequest(ModelState)); } if (!_repo.ClassEntityExists(ownerIdentityName)) { return(NotFound()); } var finalTrackedItem = _mapper.Map <Entities.TrackedItem>(newTrackedItemDto); //START: Might be able to replace this by defining student name as a Key value for EFCore var trackedItems = _repo.GetAllTrackedItemsFromClass(ownerIdentityName); foreach (var s in trackedItems) { if (s.Name == finalTrackedItem.Name) { return(UnprocessableEntity("There is already a Tracked Item with that name in this Class")); } } //STOP: Might be able to replace this by defining student name as a Key value for EFCore _repo.AddTrackedItem(ownerIdentityName, finalTrackedItem); if (!_repo.Save()) { return(StatusCode(500, $"A problem happened while handling your request to create a Tracked Item with name: {newTrackedItemDto.Name}.")); } var trackedItemToReturn = _mapper.Map <TrackedItemDto>(finalTrackedItem); return(CreatedAtAction("GetTrackedItem", new { trackedItemId = trackedItemToReturn.Id }, trackedItemToReturn)); }