public HttpResponseMessage PostLoginUser(UserModel model) { return this.PerformOperationAndHandleExceptions(() => { var context = this.ContextFactory.Create(); using (context) { var usernameToLower = model.Username.ToLower(); var usersDbSet = context.Set<User>(); var entity = usersDbSet.SingleOrDefault(usr => usr.Username == usernameToLower && usr.AuthenticationCode == model.AuthCode); if (entity == null) { var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid username or password"); throw new HttpResponseException(errResponse); } entity.SessionKey = this.GenerateSessionKey(entity.Id); context.SaveChanges(); var responseModel = new LoginResponseModel() { Nickname = entity.Username, SessionKey = entity.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Accepted, responseModel); return response; } }); }
public HttpResponseMessage PostRegisterUser(UserModel model) { return this.PerformOperationAndHandleExceptions(() => { var usernameToLower = model.Username.ToLower(); var context = this.ContextFactory.Create(); using (context) { var usersDbSet = context.Set<User>(); var entity = usersDbSet .FirstOrDefault(usr => usr.Username == usernameToLower); if (entity != null) { string responseMessage = "Username already taken"; HttpResponseMessage errResponse = this.Request.CreateErrorResponse(HttpStatusCode.Conflict, responseMessage); throw new HttpResponseException(errResponse); } entity = new User() { Username = model.Username.ToLower(), AuthenticationCode = model.AuthCode }; usersDbSet.Add(entity); context.SaveChanges(); return this.PostLoginUser(model); //return response; } }); }