// GET api/values/ public HttpResponseMessage Get(string email, string password) { try { using (SampleDbEntities entities = new SampleDbEntities()) { List<User> UsersList = entities.Users.ToList<User>(); User loginUser = UsersList.Where(u => u.Email == email && u.Password == password).FirstOrDefault(); if (loginUser == null) { return Request.CreateResponse(HttpStatusCode.InternalServerError, "Username or password is incorrect"); } else if (loginUser.Active.Trim() == "y") { return Request.CreateResponse(HttpStatusCode.OK, "Success-" + loginUser.Username); } else { return Request.CreateResponse(HttpStatusCode.InternalServerError, "Please confirm your email. "); } } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } }
// GET api/values public string Get(int userid) { using (SampleDbEntities entities = new SampleDbEntities()) { User activateUser = entities.Users.Where(a => a.Id == userid).FirstOrDefault(); if(activateUser != null) { activateUser.Active = "y"; entities.SaveChanges(); } } return string.Empty; }
// POST api/values public HttpResponseMessage Post(User value) { try { if (ModelState.IsValid) { using (SampleDbEntities entities = new SampleDbEntities()) { if (CheckForUsernameAlreadyExists(value, entities)) { string error = string.Format("User with {0} username already exist", value.Username); return Request.CreateResponse(HttpStatusCode.InternalServerError, error); } else if (CheckForEmailAlreadyExists(value, entities)) { string error = string.Format("User with {0} email already exist", value.Email); return Request.CreateResponse(HttpStatusCode.InternalServerError, error); } else { value.Active = "n"; entities.Users.Add(value); entities.SaveChanges(); SendActiveLinkToUser(value); return Request.CreateResponse(HttpStatusCode.OK, "Success"); } } } else { return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid"); } } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError, "Some error occrued in registration" ); } }
private bool CheckForUsernameAlreadyExists(User user, SampleDbEntities entities) { bool isExists = false; try { List<User> UsersList = entities.Users.ToList<User>(); User loginUser = UsersList.Where(u => u.Username == user.Username).FirstOrDefault(); if (loginUser == null) isExists = false; else isExists = true; } catch (Exception) { isExists = true; } return isExists; }