private Material BuildMaterial(Server.Models.Material material) { var authors = material.MaterialAuthors .Select(ma => new Author { AuthorId = ma.Author.AuthorId, FirstName = ma.Author.FirstName, LastName = ma.Author.LastName, }) .ToList(); var subjects = material.MaterialSubjects .Select(ms => new MaterialSubject { SubjectId = ms.MaterialSubject.SubjectId, SubjectName = ms.MaterialSubject.SubjectName, }) .ToList(); var materialType = new MaterialType { TypeId = material.Type.TypeId, Type = material.Type.Type }; return(new Material { MaterialId = material.MaterialId, Isbn = material.Isbn, Title = material.Title, Language = material.Language, Description = material.Description, Type = materialType, Authors = authors, MaterialSubjects = subjects, }); }
public IActionResult UpdateMaterial(int id, [FromBody] Material newMaterial) { var material = _controller.FindByID(id); if (material == null) { return(new NotFoundResult()); } // build server data var materialType = new Server.Models.MaterialType { TypeId = newMaterial.Type.TypeId, Type = newMaterial.Type.Type }; var materialSubjects = newMaterial.MaterialSubjects .Select(subject => new Server.Models.MaterialSubjects { MaterialSubject = new Server.Models.MaterialSubject { SubjectName = subject.SubjectName, SubjectId = subject.SubjectId }, Material = material }) .ToList(); var materialAuthors = newMaterial.Authors .Select(author => new Server.Models.MaterialAuthor { Material = material, Author = new Server.Models.Author { AuthorId = author.AuthorId, FirstName = author.FirstName, LastName = author.LastName } }) .ToList(); var newServerMaterial = new Server.Models.Material { Isbn = newMaterial.Isbn, Title = newMaterial.Title, Language = newMaterial.Language, Description = newMaterial.Description, Lendable = newMaterial.Lendable, Type = materialType, MaterialAuthors = materialAuthors, MaterialSubjects = materialSubjects, }; try { var changedRows = _controller.Update(id, newServerMaterial); // TODO: Add if statement that checks for the right amount of changedRows } catch (ArgumentException e) { return(BadRequest(e.Message)); } material = _controller.FindByID(id); return(new JsonResult(BuildMaterial(material))); }