Beispiel #1
0
        private UserEntity.User GetPopulateData(string id, string paramAll)
        {
            string[] iParams;

            iParams = paramAll.Split('~');

            string userName    = iParams[0].ToString();
            string password    = iParams[1].ToString();
            string confirmPass = iParams[2].ToString();
            string fullname    = iParams[3].ToString();
            string noKartuId   = iParams[4].ToString();
            string email       = iParams[5].ToString();
            string roleId      = iParams[6].ToString();
            string isLogin     = iParams[7].ToString();
            string isActive    = iParams[8].ToString();

            userInfo = new UserEntity.User();

            if (!string.IsNullOrEmpty(id) && id != "0")
            {
                userInfo.ID = Convert.ToInt16(id);
            }

            userInfo.Code     = roleId;
            userInfo.UserName = userName;
            userInfo.Password = Functions.HashPasswordSha256(password);
            userInfo.Fullname = fullname;
            userInfo.IDKartu  = Convert.ToInt16(noKartuId);
            userInfo.Email    = email;
            userInfo.IDRole   = Convert.ToInt16(roleId);
            userInfo.IsLogin  = Convert.ToBoolean(isLogin);
            userInfo.IsActive = Convert.ToBoolean(isActive);

            if (!string.IsNullOrEmpty(id) && id != "0")
            {
                HttpClient client = new HttpClient();

                string baseUrl = Url.Action("", "", null, HttpContext.Request.Scheme);
                client.BaseAddress = new Uri(baseUrl);

                var text       = client.GetStringAsync("api/UserApi/GetList").Result;
                var resultUser = JsonConvert.DeserializeObject <List <UserEntity.User> >(text);

                userInfo.LastLoginDate = resultUser.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().LastLoginDate;
                userInfo.CreatedDate   = resultUser.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().CreatedDate;
                userInfo.CreatedBy     = resultUser.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().CreatedBy;
                userInfo.UpdatedDate   = DateTime.Now;
                userInfo.UpdatedBy     = "System";
            }
            else
            {
                //userInfo.LastLoginDate = userInfo.LastLoginDate;
                userInfo.CreatedDate = DateTime.Now;
                userInfo.CreatedBy   = "System";
                userInfo.UpdatedDate = null;
                userInfo.UpdatedBy   = null;
            }

            return(userInfo);
        }
Beispiel #2
0
        public ActionResult DeleteUser(string id = "")
        {
            int    userId = 0;
            object result = null;


            try
            {
                userId = Convert.ToInt16(id);

                userInfo    = new UserEntity.User();
                userInfo.ID = userId;

                // User By Id
                HttpClient client = new HttpClient();

                string baseUrl = Url.Action("", "", null, HttpContext.Request.Scheme);
                client.BaseAddress = new Uri(baseUrl);

                var putTask = client.DeleteAsync("api/UserApi/DeleteById=" + id);
                putTask.Wait();


                result = new { error = 0 };
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.Message, hosting);
            }

            return(Json(result));
        }
Beispiel #3
0
        public void Post([FromBody] UserEntity.User user)
        {
            context.Entry(user).State = EntityState.Added;
            context.Users.Add(user);
            context.SaveChanges();

            CreatedAtAction(nameof(user), new { id = user.ID }, user);
        }
Beispiel #4
0
        public void Put(long id, [FromBody] UserEntity.User user)
        {
            if (id != user.ID)
            {
                BadRequest();
                return;
            }

            context.Entry(user).State = EntityState.Modified;
            context.Update(user);
            context.SaveChanges();
        }
Beispiel #5
0
        public ActionResult AddEditUser(string id, string paramAll)
        {
            object result = null;

            try
            {
                string[] iParams;
                iParams = paramAll.Split('~');

                string userName    = iParams[0].ToString();
                string password    = iParams[1].ToString();
                string confirmPass = iParams[2].ToString();
                string email       = iParams[5].ToString();


                HttpClient client = new HttpClient();

                string baseUrl = Url.Action("", "", null, HttpContext.Request.Scheme);
                client.BaseAddress = new Uri(baseUrl);

                var text       = client.GetStringAsync("api/UserApi/GetList").Result;
                var resultUser = JsonConvert.DeserializeObject <List <UserEntity.User> >(text);

                int countUserName = resultUser.Where(x => x.UserName == userName.Trim()).Count();
                int countEmail    = resultUser.Where(x => x.Email == email.Trim()).Count();

                // is valid email
                bool isValidEmail = Functions.IsValidEmail(email);

                // is valid alpha numeric
                bool isValidAlphaNumeric = Functions.IsAlphaNumeric(password);

                // If data empty
                bool isFieldNull = false;
                for (int x = 1; x < iParams.Count() - 2; x++)
                {
                    // Mandatory Field
                    if (x != 3)
                    {
                        if (string.IsNullOrEmpty(iParams[x].ToString()))
                        {
                            isFieldNull = true;
                            break;
                        }
                    }
                }

                if (!string.IsNullOrEmpty(id) && id != "0")
                {
                    string userNameEdit = resultUser.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().UserName;
                    string emailEdit    = resultUser.Where(x => x.ID == Convert.ToInt16(id)).FirstOrDefault().Email;

                    if (isFieldNull)
                    {
                        result = new { error = 1 }
                    }
                    ;
                    else if (userNameEdit != userName && countUserName > 0)
                    {
                        result = new { error = 2 }
                    }
                    ;
                    else if (!isValidAlphaNumeric)
                    {
                        result = new { error = 3 }
                    }
                    ;
                    else if (password.Trim() != confirmPass.Trim())
                    {
                        result = new { error = 4 }
                    }
                    ;
                    else if (emailEdit != email && countEmail > 0)
                    {
                        result = new { error = 5 }
                    }
                    ;
                    else if (!isValidEmail)
                    {
                        result = new { error = 6 }
                    }
                    ;
                    else
                    {
                        // Edit User
                        userInfo = new UserEntity.User();
                        userInfo = GetPopulateData(id, paramAll);

                        var jsonString = JsonConvert.SerializeObject(userInfo);
                        var putTask    = client.PutAsync("api/UserApi/UpdateById=" + id, new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json"));
                        putTask.Wait();

                        result = new { error = "Edit" };
                    }
                }
                else
                {
                    if (isFieldNull)
                    {
                        result = new { error = 1 }
                    }
                    ;
                    else if (countUserName > 0)
                    {
                        result = new { error = 2 }
                    }
                    ;
                    else if (!isValidAlphaNumeric)
                    {
                        result = new { error = 3 }
                    }
                    ;
                    else if (password.Trim() != confirmPass.Trim())
                    {
                        result = new { error = 4 }
                    }
                    ;
                    else if (countEmail > 0)
                    {
                        result = new { error = 5 }
                    }
                    ;
                    else if (!isValidEmail)
                    {
                        result = new { error = 6 }
                    }
                    ;
                    else
                    {
                        // Add User
                        userInfo = new UserEntity.User();
                        userInfo = GetPopulateData(id, paramAll);

                        var jsonString = JsonConvert.SerializeObject(userInfo);
                        var putTask    = client.PostAsync("api/UserApi/CreateNew", new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json"));
                        putTask.Wait();

                        result = new { error = "Add" };
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.Message, hosting);
            }

            return(Json(result));
        }