Ejemplo n.º 1
0
        public IHttpActionResult AddRolesByEmployeeId(int id, [FromBody] EmployeeRoleModels.Edit roles)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            using (var ctx = new SFDatabaseEntities())
            {
                foreach (var roleId in roles.UserRoleId)
                {
                    EmployeeInRole newEmployeeRole = new EmployeeInRole()
                    {
                        EmployeeID  = id,
                        UserRoleID  = roleId,
                        CreatedAt   = DateTime.Parse(DateTime.Now.ToString()),
                        DeletedFlag = false
                    };
                    ctx.EmployeeInRole.Add(newEmployeeRole);
                }

                StringBuilder logMessage = new StringBuilder();
                try
                {
                    ctx.SaveChanges();
                    RedisCacheHelper.DeleteEmployeeCache(id);

                    logMessage.AppendLine("(AddRolesByEmployeeId) Delete EmployCache: " + id);
                    Startup._sfAppLogger.Debug(logMessage);
                    return(Ok());
                }
                catch (Exception ex)
                {
                    logMessage.AppendLine("(AddRolesByEmployeeId) Excepton on Delete EmployCache: " + id + "; Exception:" + ex.Message);
                    Startup._sfAppLogger.Error(logMessage);
                    return(InternalServerError());
                }
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult EditRolesByEmployeeId(int id, [FromBody] EmployeeRoleModels.Edit roles)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            using (var ctx = new SFDatabaseEntities())
            {
                var existingRoles = ctx.EmployeeInRole
                                    .Where(s => s.EmployeeID == id)
                                    .Select(s => s).ToList();

                //調整現有的Role
                List <int> existingRolesId = new List <int>();
                if (existingRoles != null)
                {
                    foreach (var er in existingRoles)
                    {
                        if (roles == null || (!roles.UserRoleId.Contains(er.UserRoleID) && !er.DeletedFlag))
                        {
                            er.DeletedFlag = true;
                            er.UpdatedAt   = DateTime.Parse(DateTime.Now.ToString());
                        }
                        else if (roles.UserRoleId.Contains(er.UserRoleID) && er.DeletedFlag)
                        {
                            er.DeletedFlag = false;
                            er.UpdatedAt   = DateTime.Parse(DateTime.Now.ToString());
                        }

                        existingRolesId.Add(er.UserRoleID);
                    }
                }
                //新增沒有的Role
                if (roles != null)
                {
                    foreach (var userRoleId in roles.UserRoleId)
                    {
                        if (existingRoles == null || (userRoleId > 0 && !existingRolesId.Contains(userRoleId)))
                        {
                            var newEmployeeRole = new EmployeeInRole()
                            {
                                EmployeeID = id,
                                UserRoleID = userRoleId,
                                CreatedAt  = DateTime.Parse(DateTime.Now.ToString())
                            };
                            ctx.EmployeeInRole.Add(newEmployeeRole);
                        }
                    }
                }

                StringBuilder logMessage = new StringBuilder();
                try
                {
                    ctx.SaveChanges();
                    RedisCacheHelper.DeleteEmployeeCache(id);

                    logMessage.AppendLine("(EditRolesByEmployeeId )Delete EmployCache: " + id);
                    Startup._sfAppLogger.Debug(logMessage);
                    return(Ok());
                }
                catch (Exception ex)
                {
                    logMessage.AppendLine("(EditRolesByEmployeeId) Excepton on Delete EmployCache: " + id + "; Exception:" + ex.Message);
                    Startup._sfAppLogger.Error(logMessage);
                    return(InternalServerError());
                }
            }
        }