public async Task CreateStudent() { var request = new StudentCreation(); var controller = new StudentController(context, new NullLogger <StudentController>()); var result = await controller.Post(request).ConfigureAwait(false); result.Should().BeOfType <CreatedAtRouteResult>(); //context.Students.First().StudentId.Should().Be(1); }
public async Task PostStudentBadRequest() { var request = new StudentCreation(); var controller = new StudentController(context, new NullLogger <StudentController>()); controller.ControllerContext.ModelState.AddModelError(string.Empty, "Error"); var result = await controller.Post(request).ConfigureAwait(false); result.Should().BeOfType <BadRequestObjectResult>(); }
public async Task PostStudentThrowsInvalidOperationExecption() { var options = new DbContextOptionsBuilder <PhsomContext>().UseInMemoryDatabase("KLS").Options; context = new PhsomContext(options); var request = new StudentCreation(); var controller = new StudentController(context, new NullLogger <StudentController>()); await controller.Post(new StudentCreation()).ConfigureAwait(false); request.StudentId = 1; var result = await controller.Post(request).ConfigureAwait(false); result.Should().BeOfType <ObjectResult>(); ((ObjectResult)result).StatusCode.Should().Be((int)HttpStatusCode.InternalServerError); }
///// <summary> ///// ///// </summary> ///// <param name="id"></param> ///// <returns></returns> //[EnableQuery] //public SingleResult<StudentCreation> Get([FromODataUri] int id) //{ // var student = phsomContext.Students.Where(x => x.StudentId == id).Select(x => Mapper.Map<StudentCreation>(x)).AsQueryable(); // return SingleResult.Create(student); //} /// <summary> /// The post. /// </summary> /// <param name="request"> /// The request. /// </param> /// <returns> /// The <see cref="Task" />. /// </returns> public async Task <IActionResult> Post(StudentCreation request) { if (request == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { await phsomContext.Students.AddAsync(Mapper.Map <Student>(request)).ConfigureAwait(false); await phsomContext.SaveChangesAsync().ConfigureAwait(false); } catch (ArgumentException ex) { logger.LogError(ex, "An error occurred saving the student"); return(StatusCode((int)HttpStatusCode.InternalServerError, "An error occurred saving the student")); } catch (InvalidOperationException ex) { logger.LogError(ex, "An error occurred saving the student"); return(StatusCode((int)HttpStatusCode.InternalServerError, "An error occurred saving the student")); } catch (DbUpdateException ex) { logger.LogError(ex, "An error occurred saving the student"); return(StatusCode((int)HttpStatusCode.InternalServerError, "An error occurred saving the student")); } return(CreatedAtRoute(nameof(this.Get), new { id = request.StudentId }, request)); }