示例#1
0
        public IHttpActionResult Get(int id)
        {
            Department department = db.Departments.Find(id);

            if (department == null)
            {
                return(NotFound());
            }

            var model = new DepartmentApiModel {
                Id = department.Id, Name = department.Name
            };

            return(Ok(model));
        }
示例#2
0
        public IHttpActionResult Delete(int id)
        {
            Department department = db.Departments.Find(id);

            if (department == null)
            {
                return(NotFound());
            }

            db.Departments.Remove(department);
            db.SaveChanges();

            var model = new DepartmentApiModel {
                Id = department.Id, Name = department.Name
            };

            return(Ok(model));
        }
示例#3
0
        public IHttpActionResult PostDepartment(DepartmentApiModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Department department = new Department {
                Name = model.Name
            };

            db.Departments.Add(department);
            db.SaveChanges();

            // Get the new Id from the database
            model.Id = department.Id;

            // Return status code 201 success with creation
            return(CreatedAtRoute("DefaultApi", new { id = department.Id }, model));
        }
示例#4
0
        public IHttpActionResult Put(int id, DepartmentApiModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != model.Id)
            {
                return(BadRequest());
            }

            var department = new Department {
                Id = model.Id, Name = model.Name
            };

            db.Entry(department).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DepartmentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }