public HttpResponseMessage RegisterUser([FromBody]UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BGNewsDB(); using (context) { UserPersister.ValidateUsername(model.Username); UserPersister.ValidateNickname(model.DisplayName); UserPersister.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var nicknameToLower = model.DisplayName.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower || usr.DisplayName.ToLower() == nicknameToLower); if (user != null) { throw new InvalidOperationException("User exists"); } user = new User() { Username = usernameToLower, DisplayName = model.DisplayName, AuthCode = model.AuthCode, ProfilePictureUrlMedium = "http://farm6.static.flickr.com/5505/9654705460_5a5026dc3e_m.jpg", ProfilePictureUrlThumbnail = "http://static.flickr.com/5505/9654705460_5a5026dc3e_s.jpg" }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = UserPersister.GenerateSessionKey(user.UserId); context.SaveChanges(); var loggedModel = new UserLoggedModel() { DisplayName = user.DisplayName, SessionKey = user.SessionKey, ProfilePictureMediumUrl = user.ProfilePictureUrlMedium, ProfilePictureThumbnailUrl = user.ProfilePictureUrlThumbnail }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public HttpResponseMessage PostLoginUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BGNewsDB(); using (context) { UserPersister.ValidateUsername(model.Username); UserPersister.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower && usr.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } if (user.SessionKey == null) { user.SessionKey = UserPersister.GenerateSessionKey(user.UserId); context.SaveChanges(); } var loggedModel = new UserLoggedModel() { DisplayName = user.DisplayName, SessionKey = user.SessionKey, ProfilePictureMediumUrl = "http://farm6.static.flickr.com/5505/9654705460_5a5026dc3e_m.jpg", ProfilePictureThumbnailUrl = "http://static.flickr.com/5505/9654705460_5a5026dc3e_s.jpg" }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }