コード例 #1
0
 public SchoolModel GetById(int id)
 {
     var responseMsg = this.PerformOperationAndHandleExceptions(() =>
     {
         var result = new SchoolModel();
         var db = new SchoolContext();
         var school = db.Schools.Find(id);
         if (school != null)
         {
             result.Id = school.Id;
             result.Location = school.Location;
             result.Name = school.Name;
         }
         return result;
     });
     return responseMsg;
 }
コード例 #2
0
        public HttpResponseMessage Post(SchoolModel school)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var newcSchool = new School()
                {
                    Location = school.Location,
                    Name = school.Name
                };
                var db = new SchoolContext();
                db.Schools.Add(newcSchool);
                db.SaveChanges();

                school.Id = newcSchool.Id;
                var response = this.Request.CreateResponse(HttpStatusCode.Created, school);
                return response;
            });
            return responseMsg;
        }
コード例 #3
0
        public void PostTest()
        {
            SchoolModel school = new SchoolModel()
            {
                Location = "bg",
                Name = "lelq vachka"
            };

            var httpServer = new InMemoryHttpServer("http://localhost:4728/");
            var response = httpServer.CreatePostRequest("api/school", school);

            Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            Assert.IsNotNull(response.Content);

            var contentString = response.Content.ReadAsStringAsync().Result;
            var model = JsonConvert.DeserializeObject<SchoolModel>(contentString);

            Assert.IsTrue(model.Id > 0);
        }