public HttpResponseMessage PostRegisterUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BloggingSystemContext(); using (context) { this.ValidateUsername(model.Username); this.ValidateDisplayName(model.DisplayName); this.ValidateAuthCode(model.AuthCode); var usernameToLower = model.Username.ToLower(); var displayNameToLower = model.DisplayName.ToLower(); var user = context.Users.FirstOrDefault( usr => usr.Username == usernameToLower || usr.DisplayName.ToLower() == displayNameToLower); if (user != null) { throw new InvalidOperationException("User already exists"); } user = new User() { Username = usernameToLower, DisplayName = model.DisplayName, AuthCode = model.AuthCode }; context.Users.Add(user); context.SaveChanges(); user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); var loggedModel = new LoggedUserModel() { DisplayName = user.DisplayName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public HttpResponseMessage PostLoginUser(UserModel model) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new BloggingSystemContext(); using (context) { this.ValidateUsername(model.Username); this.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 = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedModel = new LoggedUserModel() { DisplayName = user.DisplayName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } }); return responseMsg; }
public HttpResponseMessage PutLogoutUser(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BloggingSystemContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user != null) { user.SessionKey = null; context.SaveChanges(); } } var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }); return responseMsg; }
public HttpResponseMessage PutComments(int postId, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var context = new BloggingSystemContext(); var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } var commentEntities = context.Posts.FirstOrDefault(p => p.Id == postId).Comments; if (commentEntities != null) { commentEntities = commentEntities; context.SaveChanges(); } var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; }); return responseMsg; //var context = new ForumContext(); //var postEntities = context.Threads.FirstOrDefault(thr => thr.Id == threadId).Posts; ////parsing to PostModel //CommentModel[] models = { new CommentModel(){ // Text="First", // PostDate= DateTime.Now, // CommentedBy="Bai Gosho", //}}; //return models.AsQueryable(); }