public HttpResponseMessage AddUnit(Entities.MstUnit objUnit)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("SystemTables")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanAdd)
                        {
                            Data.MstUnit newUnit = new Data.MstUnit
                            {
                                Unit            = objUnit.Unit,
                                IsLocked        = true,
                                CreatedById     = currentUserId,
                                CreatedDateTime = DateTime.Now,
                                UpdatedById     = currentUserId,
                                UpdatedDateTime = DateTime.Now
                            };

                            db.MstUnits.InsertOnSubmit(newUnit);
                            db.SubmitChanges();

                            String newObject = at.GetObjectString(newUnit);
                            at.InsertAuditTrail(currentUser.FirstOrDefault().Id, GetType().Name, MethodBase.GetCurrentMethod().Name, "NA", newObject);

                            return(Request.CreateResponse(HttpStatusCode.OK, newUnit.Id));
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to add unit."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this system table page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }
Beispiel #2
0
        public HttpResponseMessage updateUnit(String id, Entities.MstUnit unit)
        {
            try
            {
                var units = from d in db.MstUnits where d.Id == Convert.ToInt32(id) select d;
                if (units.Any())
                {
                    var updateUnit = units.FirstOrDefault();
                    updateUnit.Unit = unit.Unit;
                    db.SubmitChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
            }
            catch (Exception e)
            {
                // Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
        public HttpResponseMessage UpdateUnit(Entities.MstUnit objUnit, String id)
        {
            try
            {
                var currentUser = from d in db.MstUsers
                                  where d.UserId == User.Identity.GetUserId()
                                  select d;

                if (currentUser.Any())
                {
                    var currentUserId = currentUser.FirstOrDefault().Id;

                    var userForms = from d in db.MstUserForms
                                    where d.UserId == currentUserId &&
                                    d.SysForm.FormName.Equals("SystemTables")
                                    select d;

                    if (userForms.Any())
                    {
                        if (userForms.FirstOrDefault().CanEdit)
                        {
                            var unit = from d in db.MstUnits
                                       where d.Id == Convert.ToInt32(id)
                                       select d;

                            if (unit.Any())
                            {
                                var updateUnit = unit.FirstOrDefault();
                                updateUnit.Unit            = objUnit.Unit;
                                updateUnit.IsLocked        = true;
                                updateUnit.UpdatedById     = currentUserId;
                                updateUnit.UpdatedDateTime = DateTime.Now;

                                db.SubmitChanges();

                                return(Request.CreateResponse(HttpStatusCode.OK));
                            }
                            else
                            {
                                return(Request.CreateResponse(HttpStatusCode.NotFound, "Data not found. These unit details are not found in the server."));
                            }
                        }
                        else
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no rights to edit and update unit."));
                        }
                    }
                    else
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Sorry. You have no access for this system table page."));
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Theres no current user logged in."));
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Something's went wrong from the server."));
            }
        }