Ejemplo n.º 1
0
        public IHttpActionResult EditEmployeeById(int id, [FromBody] EmployeeModels.Update employee)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string logForm          = "Form : " + js.Serialize(employee);
            string logAPI           = "[Put] " + Request.RequestUri.ToString();

            if (!ModelState.IsValid || employee == null)
            {
                Startup._sfAppLogger.Warn(logAPI + " || Input Parameter not expected || " + logForm);
                return(BadRequest("Invalid data"));
            }

            try
            {
                EmployeeModels employeeModel = new EmployeeModels();
                employeeModel.updateEmployee(id, employee);
                RedisCacheHelper.DeleteEmployeeCache(id);
                return(Ok("Success"));
            }
            catch (Exception ex)
            {
                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                logMessage.AppendLine(logForm);
                Startup._sfAppLogger.Error(logAPI + logMessage);

                return(InternalServerError(ex));
            }
        }
Ejemplo n.º 2
0
 public IHttpActionResult DeleteEmployeeById(int id)
 {
     try
     {
         EmployeeModels employeeModel = new EmployeeModels();
         employeeModel.deleteEmployee(id);
         RedisCacheHelper.DeleteEmployeeCache(id);
         return(Ok("Success"));
     }
     catch (Exception ex)
     {
         string        logAPI     = "[Delete] " + Request.RequestUri.ToString();
         StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
         Startup._sfAppLogger.Error(logAPI + logMessage);
         return(InternalServerError());
     }
 }
Ejemplo n.º 3
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.º 4
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());
                }
            }
        }
Ejemplo n.º 5
0
        public async Task <HttpResponseMessage> UploadPhotoFile(int id)
        {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
            }

            using (var ctx = new SFDatabaseEntities())
            {
                var existEmployee = ctx.Employee
                                    .Where(s => s.Id == id)
                                    .FirstOrDefault();

                if (existEmployee == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                string root     = Path.GetTempPath();
                var    provider = new MultipartFormDataStreamProvider(root);

                try
                {
                    // Read the form data.
                    string fileAbsoluteUri = "";
                    await Request.Content.ReadAsMultipartAsync(provider);

                    char[] trimChar = { '\"' };

                    //FileData
                    foreach (MultipartFileData fileData in provider.FileData)
                    {
                        string formColumnName   = fileData.Headers.ContentDisposition.Name.ToLower().Trim(trimChar);
                        string fileExtenionName = fileData.Headers.ContentDisposition.FileName.Split('.')[1].ToLower().Trim(trimChar);
                        if (formColumnName.Equals("image"))
                        {
                            if (fileExtenionName.Equals("png") || fileExtenionName.Equals("jpg"))
                            {
                                string          uploadFilePath  = "company-" + existEmployee.CompanyId + "/employee/" + id + "-default." + fileExtenionName;
                                SharedFunctions sharedFunctions = new SharedFunctions();
                                fileAbsoluteUri = sharedFunctions.SaveFiletoStorage(fileData.LocalFileName, uploadFilePath, "images");
                            }
                            else
                            {
                                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Wrong extension name"));
                            }
                        }
                    }

                    if (fileAbsoluteUri.Equals(""))
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "File is empty"));
                    }

                    //Edit employee photo path
                    existEmployee.PhotoURL = fileAbsoluteUri;
                    ctx.SaveChanges();
                    RedisCacheHelper.DeleteEmployeeCache(id);

                    var returnObj = new {
                        imageURL = fileAbsoluteUri
                    };
                    return(Request.CreateResponse(HttpStatusCode.OK, returnObj));
                }
                catch (System.Exception e)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
                }
            }
        }