Пример #1
0
        public async Task <IActionResult> Update(Guid id, Users users)//[FromBody]
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { danger = TM.Common.Error.Model }));
            }

            if (id != users.Id)
            {
                return(Json(new { danger = TM.Common.Error.Update }));
            }

            try
            {
                users.Roles     = "," + users.Roles + ",";
                users.UpdatedBy = Common.Auth.getUserAction();
                users.UpdatedAt = DateTime.Now;
                _context.Users.Attach(users);
                var entry = _context.Entry(users);
                entry.Property(m => m.FullName).IsModified  = true;
                entry.Property(m => m.Mobile).IsModified    = true;
                entry.Property(m => m.Email).IsModified     = true;
                entry.Property(m => m.Address).IsModified   = true;
                entry.Property(m => m.Details).IsModified   = true;
                entry.Property(m => m.Orders).IsModified    = true;
                entry.Property(m => m.Roles).IsModified     = true;
                entry.Property(m => m.Flag).IsModified      = true;
                entry.Property(m => m.UpdatedBy).IsModified = true;
                entry.Property(m => m.UpdatedAt).IsModified = true;

                //Create Directory Upload
                var upload = new TM.Helper.Upload(Request.Form.Files, TM.Common.Directories.Uploads + PathInUpload);
                var file   = upload.ImagesName();
                if (file.Count > 0)
                {
                    users.Images = TM.Common.Directories.Uploads + PathInUpload + file[0];
                    entry.Property(m => m.Images).IsModified = true;
                }

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Json(new { danger = TM.Common.Error.DB }));
            }

            return(Json(new { success = TM.Common.Success.Update }));
        }
Пример #2
0
        public async Task <IActionResult> Insert(Users users)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { danger = TM.Common.Error.Model }));
            }
            if (UsersNameExists(users.Username))
            {
                return(Json(new { danger = TM.Common.Error.Exist }));
            }
            try
            {
                users.Id        = Guid.NewGuid();
                users.Salt      = Guid.NewGuid().ToString();
                users.Password  = TM.Encrypt.MD5.CryptoMD5TM(users.Password + users.Salt);
                users.Roles     = "";
                users.CreatedBy = Common.Auth.getUserAction();
                users.CreatedAt = DateTime.Now;
                users.UpdatedBy = Common.Auth.getUserAction();
                users.UpdatedAt = DateTime.Now;

                //Create Directory Upload
                var upload = new TM.Helper.Upload(Request.Form.Files, TM.Common.Directories.Uploads + PathInUpload);
                var file   = upload.ImagesName();
                if (file.Count > 0)
                {
                    users.Images = TM.Common.Directories.Uploads + PathInUpload + file[0];
                }

                _context.Users.Add(users);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (UsersExistsID(users.Id))
                {
                    return(Json(new { danger = TM.Common.Error.IDExist }));
                }
                else
                {
                    return(Json(new { danger = TM.Common.Error.DB }));
                }
            }
            return(Json(new { success = TM.Common.Success.Create }));
        }
Пример #3
0
        public async Task <IActionResult> PutUsersProfile(Users users)//[FromBody]
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { danger = TM.Common.Error.Model }));
            }
            if (users.Id != Common.Auth.AuthUser.Id && await _context.Users.SingleOrDefaultAsync(m => m.Id == Common.Auth.AuthUser.Id) == null)
            {
                return(Json(new { success = TM.Common.Success.Update }));
            }
            try
            {
                users.UpdatedBy = Common.Auth.getUserAction();
                users.UpdatedAt = DateTime.Now;
                _context.Users.Attach(users);
                var entry = _context.Entry(users);
                entry.Property(m => m.FullName).IsModified  = true;
                entry.Property(m => m.Mobile).IsModified    = true;
                entry.Property(m => m.Email).IsModified     = true;
                entry.Property(m => m.Address).IsModified   = true;
                entry.Property(m => m.Details).IsModified   = true;
                entry.Property(m => m.Images).IsModified    = true;
                entry.Property(m => m.UpdatedBy).IsModified = true;
                entry.Property(m => m.UpdatedAt).IsModified = true;
                //Create Directory Upload
                var upload = new TM.Helper.Upload(Request.Form.Files, TM.Common.Directories.Uploads + PathInUpload);
                var file   = upload.ImagesName();
                if (file.Count > 0)
                {
                    users.Images = TM.Common.Directories.Uploads + PathInUpload + file[0];
                    entry.Property(m => m.Images).IsModified = true;
                }
                await _context.SaveChangesAsync();

                //Update Session User
                Common.Auth.SetAuth(await _context.Users.SingleOrDefaultAsync(m => m.Id == Common.Auth.AuthUser.Id));
            }
            catch (Exception)
            {
                return(Json(new { danger = TM.Common.Error.DB }));
            }
            return(Json(new { success = TM.Common.Success.Update }));
        }