public HttpResponseMessage PostRegisterUser(UserModel model) { try { var dbContext = new TheSocialEventsContext(); using (dbContext) { this.ValidateEmail(model.Email); this.ValidateName(model.FullName); var user = dbContext.Users.FirstOrDefault(u => u.Email == model.Email); if (user != null) { throw new InvalidOperationException("Users exists"); } user = new User() { Email = model.Email, FullName = model.FullName, AuthCode = model.AuthCode, PictureUrl = model.PictureUrl }; dbContext.Users.Add(user); dbContext.SaveChanges(); if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); dbContext.SaveChanges(); } user.SessionKey = this.GenerateSessionKey(user.Id); dbContext.SaveChanges(); var loggedModel = new LoggedUserModel() { FullName = user.FullName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage PostLoginUser(UserModel model) { try { ValidateEmail(model.Email); var context = new TheSocialEventsContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.Email == model.Email && u.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() { FullName = user.FullName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }