Example #1
0
        public User Update(PersonUserDTO dto, long userId)
        {
            using (var transac = new TransactionScope())
            {
                var exist  = Manager.Query.Value.All().FirstOrDefault(scope => scope.UserId == dto.UserId);
                var person = Manager.PersonManager.Value.Query.Value.All().FirstOrDefault(scope => scope.PersonId == exist.PersonId);

                if (exist == null)
                {
                    throw new Exception("You are don't exist");
                }
                else
                {
                    string hashedPass;
                    if (dto.Password == null || string.IsNullOrEmpty(dto.Password))
                    {
                        hashedPass = exist.Password;
                    }
                    else
                    {
                        hashedPass = Manager.Encrypt.Value.Encrypt(dto.Password);
                    }

                    // update user
                    exist.Username    = dto.Username;
                    exist.Password    = hashedPass;
                    exist.IMEI        = dto.IMEI;
                    exist.Status      = (int)dto.Status;
                    exist.RoleId      = dto.RoleId;
                    exist.UpdatedBy   = (int)userId;
                    exist.UpdatedDate = DateTime.UtcNow;

                    // update person
                    person.Name        = dto.Name;
                    person.Address     = dto.Address;
                    person.DateOfBirth = dto.DateOfBirth;
                    person.Gender      = dto.Gender;
                    person.UpdatedBy   = (int)userId;
                    person.UpdatedDate = DateTime.UtcNow;
                    Manager.Database.SaveChanges();
                    Manager.PersonManager.Value.Database.SaveChanges();
                }

                transac.Complete();

                return(exist);
            }
        }
Example #2
0
        public User Save(PersonUserDTO dto, long userId)
        {
            using (var transac = new TransactionScope())
            {
                var date = DateTime.UtcNow;

                var newPerson = new Person
                {
                    Name        = dto.Name,
                    Gender      = dto.Gender,
                    DateOfBirth = dto.DateOfBirth,
                    Address     = dto.Address,
                    CreatedBy   = (int)userId,
                    CreatedDate = date,
                    UpdatedBy   = (int)userId,
                    UpdatedDate = date
                };

                string passHashed = Encrypt(dto.Password);

                var newEntity = new User
                {
                    Username    = dto.Username,
                    Password    = passHashed,
                    IMEI        = dto.IMEI,
                    Status      = (int)dto.Status,
                    RoleId      = dto.RoleId,
                    CreatedBy   = (int)userId,
                    CreatedDate = date,
                    UpdatedBy   = (int)userId,
                    UpdatedDate = date
                };

                newPerson.Users.Add(newEntity);
                Manager.Database.People.Add(newPerson);
                Manager.Database.SaveChanges();
                transac.Complete();

                return(newEntity);
            }
        }
Example #3
0
        public IHttpActionResult Put([FromBody] PersonUserDTO dto)
        {
            var user = ActionContext.ActionArguments["UserDTO"] as UserDTO;

            if (user == null)
            {
                resp.Message     = "user doesn't exist";
                resp.MessageCode = 500;
                resp.ErrorStatus = !false;
                resp.ErrorCode   = 1;
                return(Ok(resp));
            }

            try
            {
                using (var manager = new UserAdapter())
                {
                    var response = manager.Updater.Value.Update(dto, user.UserId);
                    var result   = manager.Query.Value.TransformPost(response.UserId);

                    resp.Message     = "Data Updated";
                    resp.MessageCode = 200;
                    resp.ErrorStatus = false;
                    resp.ErrorCode   = 0;
                    resp.Data        = manager.Query.Value.TransformJoinUser(response.UserId);
                }
            }
            catch (Exception ex)
            {
                resp.Message     = ex.Message;
                resp.MessageCode = 500;
                resp.ErrorStatus = true;
                resp.ErrorCode   = 1;
            }
            return(Ok(resp));
        }