Exemplo n.º 1
0
        public IHttpActionResult createUnit([FromBody] UnitsviewModel model)
        {
            var logInUserName = RequestContext.Principal.Identity.Name;

            try
            {
                if (model.unit_name != null && model.dept_id != null)
                {
                    var dp = db.department.Find(model.dept_id);

                    var ct = new dept_units();
                    ct.unit_name = model.unit_name;
                    ct.id        = string.Concat("DU-", rd.Next(1000));
                    ct.dept_id   = model.dept_id;
                    db.dept_unit.Add(ct);
                    db.SaveChanges();
                    ulog.loguserActivities(logInUserName, "Added: '" + ct.unit_name + "' to '" + dp.dept_name + "' department.");
                    return(Content(HttpStatusCode.OK, "Unit has been successfully created"));
                }
                else
                {
                    return(Content(HttpStatusCode.BadRequest, "Operation fail: "));
                }
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.BadRequest, "Operation fail: " + ex));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage updateUnit([FromBody] UnitsviewModel model)
        {
            var logInUserName = RequestContext.Principal.Identity.Name;

            try
            {
                if (model.unit_name != null && model.dept_id != null && model.id != null)
                {
                    var dp = db.department.Find(model.dept_id);

                    var ct = db.dept_unit.Find(model.id);
                    if (ct != null)
                    {
                        ulog.loguserActivities(logInUserName, "User Changed unit name: '" + ct.unit_name + "' to '" + model.unit_name + "' and assign it to '" + dp.dept_name + "' department");
                        ct.unit_name = model.unit_name;
                        ct.dept_id   = model.dept_id;
                        db.SaveChanges();
                        return(Request.CreateResponse(HttpStatusCode.OK, "unit details has been updated successfully!"));
                    }
                    else
                    {
                        //  ulog.loguserActivities(logInUserName, "Units update fail");
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Units update fail"));
                    }
                }
                //  ulog.loguserActivities(logInUserName, "Units update fail");
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Units Update fail"));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }