// PUT api/Department/5
        public HttpResponseMessage PutDepartment(int id, Department department)
        {
            var db = ServicesContext.Current;
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != department.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        // POST api/Department
        public HttpResponseMessage PostDepartment(Department department)
        {
            var db = ServicesContext.Current;
            if (ModelState.IsValid)
            {
                db.Departments.Add(department);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, department);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = department.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Esempio n. 3
0
 internal void Update(string action, GZJContext context)
 {
     var _entity = context.Departments.SingleOrDefault(t =>t.Id==Id);
     if(action=="addnew"||action=="edit")
     {
         if(string.IsNullOrWhiteSpace(DepartmentName))
             throw new Exception("岗位名称不能为空");
         if(CompanyId<=0)
             throw new Exception("公司名称不能为空");
     }
     if (action == "addnew")
     {
         if (_entity != null)
             throw new Exception("该岗位当前已存在");
         _entity=context.Departments.SingleOrDefault(t=>t.DepartmentName==DepartmentName);
         if(_entity!=null)
             throw new Exception("该岗位当前已存在");
         _entity = new Department
         {
             CompanyId = CompanyId,
             DepartmentName = DepartmentName,
             Memo = Memo,
             ContactPhone=ContactPhone
         };
         context.Departments.Add(_entity);
     }
     else if (action == "edit")
     {
         if (_entity == null)
             throw new Exception("该岗位当前并不存在");
         _entity.CompanyId = CompanyId;
         _entity.DepartmentName = DepartmentName;
         _entity.ContactPhone = ContactPhone;
         _entity.Memo = Memo;
     }
     else if (action == "remove")
     {
         if (_entity.Users.Count > 0)
             throw new Exception("该岗位下含有多个用户,请先指定用户为其他岗位!");
         if(_entity==null)
             throw new Exception("该岗位当前并不存在");
         context.Departments.Remove(_entity);
     }
 }