public IActionResult CreateAuthor([FromBody] Author authorData) { if (authorData.FirstName.IsNullOrEmpty() || authorData.LastName.IsNullOrEmpty()) { return(new BadRequestResult()); } var author = _controller.Create(authorData.FirstName, authorData.LastName); var rowsChanged = _controller.Insert(author); if (rowsChanged != 1) { // log error, maybe even stop the program as it's in an inconsistent state? return(new StatusCodeResult(500)); } var uri = new Uri($"http://{Request.Host}/api/author/{author.AuthorId}"); return(new CreatedResult(uri, author)); }
/// <summary> /// Creates a material entity based on the parameters passed /// </summary> /// <param name="isbn">The ISBN of the material</param> /// <param name="title">The title of the material</param> /// <param name="language">The language the material is written in</param> /// <param name="lendable">Whether the material is lendable or not</param> /// <param name="description">A description of the material</param> /// <param name="type">The type of the material</param> /// <param name="subjects">The subjects the material covers</param> /// /// <param name="authors"> /// The authors of the material. If no AuthorId is supplied with any of the authors, /// that author WILL be inserted into the database as a new entry, even if there already exists an author with that full name. /// </param> /// /// <returns>A material object</returns> /// <exception cref="ArgumentNullException">Thrown if one of isbn, title, language or description is empty or null</exception> /// <exception cref="ArgumentException">Thrown if MaterialType, MaterialSubject or Author doesn't exist in the database</exception> public Material Create(string isbn, string title, string language, bool lendable, string description, MaterialType type, List <MaterialSubject> subjects, List <Author> authors) { // validate material data if (isbn.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(isbn), "ISBN can't be null or empty"); } if (title.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(title), "Title can't be null or empty"); } if (language.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(language), "Language can't be null or empty"); } if (description.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(description), "Description can't be null or empty"); } var materialType = _context.MaterialTypes.Find(type.TypeId); if (materialType == null) { throw new ArgumentException("MaterialType must already exist", nameof(type)); } // create material var material = new Material { Isbn = isbn, Title = title, Language = language, Lendable = lendable, Description = description, Type = materialType, }; // validate extra data var materialSubjects = subjects .Select(subject => { var s = _context.MaterialSubjects.Find(subject.SubjectId); if (s == null) { throw new ArgumentException("All MaterialSubjects must already exist", nameof(subjects)); } return(new MaterialSubjects { MaterialSubject = s, Material = material }); }).ToList(); var materialAuthors = authors .Select(author => { var fetchedAuthor = _authorController.FindByID(author.AuthorId); if (fetchedAuthor == null) { if (author.FirstName.IsNullOrEmpty() || author.LastName.IsNullOrEmpty()) { throw new ArgumentNullException( nameof(authors), "New authors must be provided with FirstName AND LastName" ); } var newAuthor = _authorController.Create(author.FirstName, author.LastName); _authorController.Insert(newAuthor); return(new MaterialAuthor { Author = newAuthor, Material = material }); } return(new MaterialAuthor { Author = fetchedAuthor, Material = material }); }).ToList(); material.MaterialSubjects = materialSubjects; material.MaterialAuthors = materialAuthors; return(material); }