public async Task <IActionResult> CreateStudent([FromBody] StudentForCreationDto student) { // if (student == null) // { // _logger.LogError("Student send from client is null."); // return BadRequest("Student is null"); // } // // if (!ModelState.IsValid) // { // _logger.LogError("Object send from client is invalid."); // return BadRequest(ModelState); // } var studentEntity = _mapper.Map <Student>(student); _repository.Student.CreateStudent(studentEntity); await _repository.SaveAsync(); var savedStudent = _mapper.Map <StudentDto>(studentEntity); _logger.LogInfo($"Student with id: {savedStudent.Id} has been created"); return(CreatedAtRoute("StudentById", new { id = savedStudent.Id }, savedStudent)); }
public async Task <IActionResult> AddStudent(Guid teacherId, [FromBody] StudentForCreationDto student) { if (student == null) { _logger.LogError("StudentForCreationDto object from client is null."); return(NotFound()); } var teacher = await _repository.Teacher.GetTeacher(teacherId, trackChanges : false); if (teacher == null) { _logger.LogInfo($"Teacher with id: {teacherId} doesn't exist in Database."); return(NotFound()); } var studentEntity = _mapper.Map <Student>(student); _repository.Student.AddStudent(teacherId, studentEntity); await _repository.Save(); var studentDto = _mapper.Map <StudentDto>(studentEntity); return(CreatedAtRoute("StudentById", new { teacherId, id = studentDto.Id }, studentDto)); }
public async Task ReturnsNoContentValidPatch(StudentForCreationDto studentForCreationDto) { var post = await this.api.CreateStudent(studentForCreationDto); var patchDoc = new JsonPatchDocument <StudentForUpdateDto>(); patchDoc.Replace(x => x.Surname, "Dummy"); //var patch = await this.api.PartiallyUpdateStudent(post.Id, patchDoc); //Assert.Null(patch); }
public async Task <StudentDto> AddStudentAsync(StudentForCreationDto student) { var studentJson = new StringContent(JsonSerializer.Serialize(student), Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync("api/students", studentJson); if (response.IsSuccessStatusCode) { return(await JsonSerializer.DeserializeAsync <StudentDto>(await response.Content.ReadAsStreamAsync())); } return(null); }
public ActionResult <StudentDto> CreateStudent(StudentForCreationDto student) { var studentEntity = _mapper.Map <Student>(student); _vejledningsbookingRepository.AddStudents(studentEntity); _vejledningsbookingRepository.Save(); var studentToReturn = _mapper.Map <StudentDto>(studentEntity); return(CreatedAtRoute("GetStudent", new { studentId = studentToReturn.Id }, studentToReturn)); }
public async Task ReturnsBadRequestFieldNotExists(StudentForCreationDto studentForCreationDto) { var post = await this.api.CreateStudent(studentForCreationDto); try { //var get = await this.api.GetStudent(post.Id, new StudentParameters { Fields = "dummy" }); } catch (ApiException validationException) { validationException.StatusCode.Should().Be(HttpStatusCode.BadRequest); } }
public async Task <IActionResult> Create([Bind("LastName,FirstNmae,EnrollmentDate")] StudentForCreationDto student) { if (ModelState.IsValid) { var studentToCreate = _mapper.Map <Student>(student); _unitOfWork.Students.Add(studentToCreate); await _unitOfWork.Complete(); var studentFromRepo = _mapper.Map <StudentsForDetailedDto>(studentToCreate); //int id = studentToCreate.Id; return(RedirectToAction(nameof(Index))); } return(View(student)); }
public async Task <ActionResult <StudentDto> > CreateStudent(StudentForCreationDto student) { var studentEntity = _mapper.Map <Student>(student); _repo.AddStudent(studentEntity); await _repo.SaveAsync(); var studentWithHouseName = await _repo.GetStudentByIdAsync(studentEntity.Id); var studentToReturn = _mapper.Map <StudentDto>(studentWithHouseName); return(CreatedAtRoute("GetStudent", new { studentId = studentToReturn.Id }, studentToReturn)); }
public async Task RemoveStudent(StudentForCreationDto studentForCreationDto) { var post = await this.api.CreateStudent(studentForCreationDto); //await this.api.DeleteStudent(post.Id); //try //{ // var get = await this.api.GetStudent(post.Id, new StudentParameters()); //} //catch (ApiException validationException) //{ // validationException.StatusCode.Should().Be(HttpStatusCode.NotFound); //} }
public IActionResult CreateStudent([FromBody] StudentForCreationDto studentModel) { if (!ModelState.IsValid) { return(BadRequest()); } var studentEntity = _mapper.Map <Student>(studentModel); _repository.AddStudent(studentEntity); _repository.Save(); var studentToReturn = _mapper.Map <StudentDto>(studentEntity); return(CreatedAtRoute("GetStudent", new { studentId = studentToReturn.Id }, studentToReturn)); }
public async Task ReturnsUnprocessableEntityEqualFields(StudentForCreationDto studentForCreationDto) { var post = await this.api.CreateStudent(studentForCreationDto); //var patchDoc = new JsonPatchDocument<StudentForUpdateDto>(); //patchDoc.Replace(x => x.Name, post.Surname); //try //{ // var patch = await this.api.PartiallyUpdateStudent(post.Id, patchDoc); //} //catch (ApiException validationException) //{ // validationException.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity); //} }
public IActionResult AddUsers([FromBody] StudentForCreationDto studentDto) { if (studentDto == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } var studentEntity = Mapper.Map <Student>(studentDto); studentEntity.Alias = "@" + studentEntity.Lastname + " " + studentEntity.Name; _studentInfoRepository.AddStudent(studentEntity); if (!_studentInfoRepository.Save()) { return(StatusCode(500, "Could not save in DB")); } var CreatedUser = Mapper.Map <StudentDto>(studentEntity); return(CreatedAtRoute("GetStudent", new { studentId = studentEntity.Alias }, CreatedUser)); }
public IActionResult Post([FromBody] StudentForCreationDto studentDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var studentEntity = Mapper.Map <Student>(studentDto); studentEntity.Alias = "@" + studentEntity.Lastname + " " + studentEntity.Name; _studentInfoRepository.AddStudent(studentEntity); if (!_studentInfoRepository.Save()) { return(StatusCode(500, "Could not save in DB")); } var CreatedUser = Mapper.Map <StudentDto>(studentEntity); return(Ok());//CreatedAtRoute("coursess({key})", new { = studentEntity.Alias}, CreatedUser); }
public IActionResult CreateStudent([FromBody] StudentForCreationDto studentForCreationDto) { if (studentForCreationDto == null) { return(this.BadRequest()); } var student = this.mapper.Map <Student>(studentForCreationDto); if (!this.studentService.AddStudent(student)) { throw new Exception("Creating an Student failed on save."); } return(this.CreatedAtRoute( RouteName.GetStudent, new { id = student.Id }, student.ShapeDataWithoutParameters(this.CreateLinksStudent))); }
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 Post([FromBody] StudentForCreationDto studentDto) { IActionResult result; if (ModelState.IsValid(out result, new [] { studentDto })) { var student = _unitOfWork.StudentRepository.Add(Mapper.Map <Student>(studentDto)); if (_unitOfWork.Commit()) { result = CreatedAtRoute("GET", new { id = student.Id }, Mapper.Map <StudentDto>(student)); } else { throw new Exception("Creating entity failed"); } _logger.LogInformation($"Added a new student entity {student.Id}"); } return(result); }
public async Task ReturnsCreated(StudentForCreationDto studentForCreationDto) { var post = await this.api.CreateStudent(studentForCreationDto); Assert.NotNull(post); }
public async Task <StudentDto> AddStudent(string classEntityOwnerIdentityName, StudentForCreationDto studentToAdd) { var classEntityForCreationJson = new StringContent(JsonSerializer.Serialize(studentToAdd), Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync($"api/classentities/{classEntityOwnerIdentityName}/students", classEntityForCreationJson); if (response.IsSuccessStatusCode) { return(await JsonSerializer.DeserializeAsync <StudentDto>(await response.Content.ReadAsStreamAsync())); } return(null); }