public HttpResponseMessage LoginUser(UserModel user) { IQueryable<User> users = this.userRepository.GetAll(); var result = from u in users where u.Username == user.Username && u.Password == user.Password select u; User newUser = result.FirstOrDefault(); if (newUser != null) { var rep = new DbUsersRepository(db); var sessionKey = rep.LoginUser(user.Username, user.Password); UserLoggedModel userModel = new UserLoggedModel() { UserID = newUser.UserID, Username = newUser.Username, SessionKey = sessionKey }; var responseMsg = Request.CreateResponse(HttpStatusCode.OK, userModel); return responseMsg; } else { var responseMsg = Request.CreateResponse(HttpStatusCode.NotFound); return responseMsg; } }
// GET api/user/5 public HttpResponseMessage Get(int id) { User user = this.userRepository.Get(id); if (user != null) { UserLoggedModel curLoggedUser = new UserLoggedModel { SessionKey = user.SessionKey, UserID = user.UserID, Username = user.Username, AvatarURL = user.Avatar }; var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(curLoggedUser); var responseMsg = this.Request.CreateResponse(HttpStatusCode.OK, jsonString); return responseMsg; } else { var responseMsg = this.Request.CreateResponse(HttpStatusCode.NotFound, "User id incorrect!"); return responseMsg; } }
// GET api/user public IEnumerable<UserLoggedModel> Get() { var users = this.userRepository.GetAll().ToList(); var loggedUsers = new List<UserLoggedModel>(); foreach (var user in users) { if (user.SessionKey != null) { UserLoggedModel curLoggedUser = new UserLoggedModel { SessionKey = user.SessionKey, UserID = user.UserID, Username = user.Username, AvatarURL = user.Avatar }; loggedUsers.Add(curLoggedUser); } } return loggedUsers; }
public HttpResponseMessage RegisterUser(User user) { var rep = new DbUsersRepository(db); rep.CreateUser(user.Username, user.Password); var sessionKey = rep.LoginUser(user.Username, user.Password); var loggedUser = new UserLoggedModel() { UserID = user.UserID, Username = user.Username, SessionKey = sessionKey }; return Request.CreateResponse(HttpStatusCode.Created, loggedUser); }
public HttpResponseMessage RegisterUser(User user) { var rep = new DbUsersRepository(this.db); if (user.Username.Length <= 3 || user.Password.Length <= 3) { return this.Request.CreateResponse(HttpStatusCode.ExpectationFailed, "Username or password incorrect!"); } rep.CreateUser(user.Username, user.Password); var sessionKey = rep.LoginUser(user.Username, user.Password); var loggedUser = new UserLoggedModel() { UserID = user.UserID, Username = user.Username, SessionKey = sessionKey, AvatarURL = user.Avatar }; return this.Request.CreateResponse(HttpStatusCode.Created, loggedUser); }