Esempio n. 1
0
        public IActionResult Post([FromBody] Controllers.DTO.User.Create postUser)
        {
            if (postUser == null)
            {
                return(BadRequest());
            }
            if (String.IsNullOrEmpty(postUser.Username) || String.IsNullOrEmpty(postUser.Password))
            {
                return(BadRequest());
            }

            Models.DTO.User newUser = new Models.DTO.User()
            {
                Username = postUser.Username,
                Password = postUser.Password,
                GUID     = Guid.NewGuid().ToString(),
                Created  = DateTime.Now,
                Updated  = DateTime.Now,
                Id_state = 1
            };
            _jWTDbDBContext.Users.Add(newUser);
            _jWTDbDBContext.SaveChanges();

            return(Ok("User Created"));
        }
Esempio n. 2
0
        public async Task <IActionResult> UpdateUser(long id, [FromBody] Models.DTO.User userObj)
        {
            if (ModelState.IsValid)
            {
                var user = DbContext.Users.Include(x => x.Role).FirstOrDefault(x => x.Id == id);
                if (user == null)
                {
                    return(StatusCode(StatusCodes.Status404NotFound, id));
                }
                var role = DbContext.Roles.FirstOrDefault(x => x.Id == userObj.RoleId);
                if (role == null)
                {
                    role = user.Role;
                }

                user.Name = userObj.Name;
                user.Role = role;
                DbContext.Users.Update(user);
                await DbContext.SaveChangesAsync();

                return(Ok(userObj));
            }
            return(BadRequest(ModelState));
        }