public IActionResult CreatePublication([FromBody] PublicationInputModel body) { if (!ModelState.IsValid) { throw new ModelFormatException("Model not properly formatted"); } var entity = _publicationService.CreatePublication(body); return(CreatedAtRoute("GetPublicationById", new { publicationId = entity.Id }, null)); }
public void CreatePublication_WhenCreatedCorrectly_ReturnsCreatedResult() { var publication = new PublicationInputModel() { EditorFirstName = "Egill", EditorLastName = "Joh", Title = "Gillis pub", Journal = "World scientific", Isbn = "123456-1234", Year = 2019, Type = "electronic" }; var createdResult = _controller.CreatePublication(publication); var result = Assert.IsType <CreatedAtRouteResult>(createdResult); Assert.Equal(201, result.StatusCode); }
public Publication CreatePublication(PublicationInputModel body) { // Validate that given publication does not exists in database bool publicationsExists = _armDbContext.Publications.Any(p => p.Isbn == body.Isbn); if (publicationsExists) { throw new ModelFormatException("This publication already Exists"); } // check if we have publications in db if not set Id to 1 else find the highest Id and add 1 to it int nextInt = 0; if (_armDbContext.Publications.Count() == 0) { nextInt = 1; } else { nextInt = _armDbContext.Publications.OrderByDescending(a => a.Id).FirstOrDefault().Id + 1; } var entity = new Publication { Id = nextInt, EditorFirstName = body.EditorFirstName, EditorLastName = body.EditorLastName, Title = body.Title, Journal = body.Journal, Isbn = body.Isbn, Year = body.Year, Type = body.Type }; _armDbContext.Publications.Add(entity); _armDbContext.SaveChanges(); return(entity); }
public PublicationDto CreatePublication(PublicationInputModel body) { var entity = new Publication { EditorFirstName = body.EditorFirstName, EditorLastName = body.EditorLastName, Title = body.Title, Journal = body.Journal, Isbn = body.Isbn, Year = body.Year, Type = body.Type }; _publications.Add(entity); return(new PublicationDto() { EditorFirstName = entity.EditorFirstName, EditorLastName = entity.EditorLastName, Title = entity.Title, Isbn = entity.Isbn, Year = entity.Year, Type = entity.Type }); }
public PublicationDto CreatePublication(PublicationInputModel body) => new PublicationDto(_publicationRepo.CreatePublication(body));