示例#1
0
        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)));
            }
        }
示例#2
0
        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)));
            }
        }
示例#3
0
        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);
        }