public HttpResponseMessage LoginUser([FromBody] UserLoginModel user) { HttpResponseMessage responseMessage = this.PerformOperation(() => { UserValidator.ValidateUsername(user.Username); UserValidator.ValidateAuthenticationCode(user.AuthCode); using (var context = this.ContextFactory.Create()) { var userEntity = context.Set <User>().FirstOrDefault( u => u.AuthCode == user.AuthCode && u.Username == user.Username.ToLower()); if (userEntity == null) { throw new InvalidOperationException("User not registered!"); } userEntity.SessionKey = UserValidator.GenerateSessionKey(userEntity.ID); context.SaveChanges(); UserLoggedModel loggedUser = UsersMapper.ToModel(userEntity); return(this.Request.CreateResponse(HttpStatusCode.Created, loggedUser)); } }); return(responseMessage); }
public HttpResponseMessage RegisterUser([FromBody] UserRegisterModel user) { HttpResponseMessage responseMessage = this.PerformOperation(() => { UserValidator.ValidateUsername(user.Username); UserValidator.ValidateDisplayName(user.DisplayName); UserValidator.ValidateAuthenticationCode(user.AuthCode); using (var context = this.ContextFactory.Create()) { User exstingUserEntity = context.Set <User>().FirstOrDefault( u => u.Username == user.Username.ToLower() || u.DisplayName.ToLower() == user.DisplayName.ToLower()); if (exstingUserEntity != null) { throw new InvalidOperationException("User already exists!"); } User newUserEntity = UsersMapper.ToEntity(user); context.Set <User>().Add(newUserEntity); context.SaveChanges(); newUserEntity.SessionKey = UserValidator.GenerateSessionKey(newUserEntity.ID); context.SaveChanges(); UserLoggedModel loggedUser = UsersMapper.ToModel(newUserEntity); return(this.Request.CreateResponse(HttpStatusCode.Created, loggedUser)); } }); return(responseMessage); }