public IHttpActionResult CreateMathematician(MathematicianRepresentation representation) { Guid uniqueGuid = Guid.Empty; if (!Guid.TryParse(representation.unique, out uniqueGuid)) { return(BadRequest()); } using (var context = GetContext()) { var mathematician = LoadMathematician(context, uniqueGuid); if (mathematician == null) { mathematician = context.Mathematicians.Add(Mathematician.Create(uniqueGuid)); SetName(mathematician, representation.name); context.SaveChanges(); } return(Created( Url.GetMathematician(mathematician.Unique), CreateRepresentation(mathematician))); } }
public IHttpActionResult UpdateMathematician(string unique, MathematicianRepresentation representation) { Guid uniqueGuid = Guid.Empty; if (!Guid.TryParse(representation.unique, out uniqueGuid)) { return(BadRequest()); } using (var context = GetContext()) { var mathematician = LoadMathematician(context, uniqueGuid); if (mathematician == null) { return(NotFound()); } SetName(mathematician, representation.name); context.SaveChanges(); return(Ok( CreateRepresentation(mathematician))); } }
private MathematicianRepresentation CreateRepresentation(Mathematician mathematician) { var representation = MathematicianRepresentation.FromEntity(mathematician); representation._links = new Dictionary <string, LinkReference> { ["self"] = new LinkReference { href = Url.GetMathematician(mathematician.Unique) } }; return(representation); }