public ActionResult AddSubjects(string studentId, SubjectsToStudentOperatins subjectToAdd) { var studentFromRepo = _repository.GetStudent(studentId); if (studentId == null) { return(NotFound()); } foreach (var subjectId in subjectToAdd.Subjects) { if (_repository.SubjectExists(subjectId)) { _repository.AddSubjectToStudent(studentFromRepo.Id, subjectId); } } _repository.UpdateStudent(studentFromRepo.Id, studentFromRepo); _repository.Save(); var subjectsEntities = _repository.GetSubjectsForStudent(studentId, false); var subjectsDtos = subjectsEntities.Select(s => _mapper.Map <SubjectDto>(s)).ToList(); return(CreatedAtRoute("GetSubjectsForStudent", new { }, subjectsDtos)); // Todo GET available subjects to add }
public ActionResult <StudentDto> Create([FromBody] StudentForCreatingDto studentForCreatingDto) { var studentEntity = _mapper.Map <Entities.Student>(studentForCreatingDto); _repository.AddStudent(studentEntity); _repository.Save(); var studentToReturn = _mapper.Map <StudentDto>(studentEntity); return(CreatedAtRoute("GetStudent", new { studentId = studentToReturn.Id }, studentToReturn)); }
public ActionResult PatchSubject(string subjectId, [FromBody] JsonPatchDocument <SubjectToUpdate> patchDocument) { if (patchDocument == null) { return(BadRequest()); } var subjectFromRepo = _repository.GetSubject(subjectId); if (subjectFromRepo == null) { return(NotFound()); } var subjectToPatch = _mapper.Map <SubjectToUpdate>(subjectFromRepo); patchDocument.ApplyTo(subjectToPatch, ModelState); if (!TryValidateModel(subjectToPatch)) { return(ValidationProblem()); } _mapper.Map(subjectToPatch, subjectFromRepo); _repository.UpdateSubject(subjectFromRepo.Id, subjectFromRepo); _repository.Save(); return(NoContent()); }