public void Delete(int id) { using (var db = new CompAgriConnection()) { var user = db.User.FirstOrDefault(u => u.User_Id == id); db.User.Remove(user); db.SaveChanges(); } }
public UserDto Get(int id) { using (var db = new CompAgriConnection()) { var user = db.User.FirstOrDefault(u => u.User_Id == id); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(new UserDto(user)); } }
public IEnumerable <UserDto> Get() { using (var db = new CompAgriConnection()) { return(db.User.Select(u => new UserDto() { User_Id = u.User_Id, UserName = u.UserName, Name = u.Name, LastName = u.LastName, Email = u.Email, UserProfile_Id = u.UserProfile_Id, }).ToList()); } }
public UserDto Get() { string token = UserUtils.GetUserToken(Request); if (token == null) { throw new HttpResponseException(HttpStatusCode.Forbidden); } using (var db = new CompAgriConnection()) { var user = db.User.FirstOrDefault(u => u.Token == token); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(new UserDto(user, true)); } }
public UserDto Post([FromBody] UserDto user) { User userFromDatabase = null; using (var db = new CompAgriConnection()) { if (user.Email == null && user.UserName == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } if (user.Email != null) { userFromDatabase = db.User.FirstOrDefault(u => u.Email == user.Email); } if (userFromDatabase == null && user.UserName != null) { userFromDatabase = db.User.FirstOrDefault(u => u.UserName == user.UserName); } if (userFromDatabase == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } var hashedPassword = PasswordUtils.HashPassword(user.Password, userFromDatabase.PasswordSalt); if (hashedPassword != userFromDatabase.Password) { throw new HttpResponseException(HttpStatusCode.Forbidden); } else { userFromDatabase.Token = TokenUtils.GenerateToken(); db.SaveChanges(); LogUserLogged(userFromDatabase); var userToSend = new UserDto(userFromDatabase, true); return(userToSend); } } }
public void Delete() { string token = UserUtils.GetUserToken(Request); if (token == null) { return; } using (var db = new CompAgriConnection()) { var user = db.User.FirstOrDefault(u => u.Token == token); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } user.Token = null; db.SaveChanges(); } }
public UserDto Post([FromBody] UserDto userDto) { if (!userDto.IsValid()) { throw new HttpResponseException(HttpStatusCode.BadRequest); } using (var db = new CompAgriConnection()) { var previousUser = db.User.FirstOrDefault(u => (u.UserName != null && u.UserName == userDto.UserName) || (u.Email != null && u.Email == userDto.Email)); if (previousUser != null) { throw WebExceptionsFactory.GetUserDuplicatedException(); } var userBeforeSave = userDto.User(); userBeforeSave.PasswordSalt = PasswordUtils.GenerateSalt(); userBeforeSave.Password = PasswordUtils.HashPassword(userBeforeSave.Password, userBeforeSave.PasswordSalt); var user = db.User.Add(userBeforeSave); db.SaveChanges(); return(new UserDto(user)); } }