public HttpResponseMessage Register(RegisteredUser regUser) { HttpResponseMessage responseMessage; if (!ModelState.IsValid) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid credentials."); return(responseMessage); } if (db.Users.All().FirstOrDefault(x => x.Email == regUser.Email) != null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "User with this email already exists."); return(responseMessage); } if (db.Users.All().FirstOrDefault(x => x.Username == regUser.Username) != null) { responseMessage = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Username already exists."); return(responseMessage); } User newUser = new User(); newUser.Username = regUser.Username; newUser.Email = regUser.Email; newUser.LastLogin = DateTime.Now; newUser.AuthKey = Guid.NewGuid().ToString(); UserSecret newUserSecret = new UserSecret() { User = newUser, Usersecret = regUser.PasswordSecret }; MongoDatabase mongoDb = MongoClientFactory.GetDatabase(); var usersCollection = mongoDb.GetCollection(MongoCollections.Users); usersCollection.Save(newUser); //var findFromMongo = usersCollection.FindOne(Query.EQ("Username", newUser.Username)); //newUser.MongoId = findFromMongo["_id"].AsString; newUser.MongoId = newUser._MongoId.ToString(); db.UserSecrets.Add(newUserSecret); db.Users.Add(newUser); db.SaveChanges(); LoginResult logResult = new LoginResult() { AuthKey = newUser.AuthKey, LastLogged = newUser.LastLogin.ToString("yyyy-MM-dd HH:mm:ss") }; responseMessage = this.Request.CreateResponse(HttpStatusCode.OK, logResult); return(responseMessage); }
public UserController(IUoWData db) { this.db = db; this.mongoDb = MongoClientFactory.GetDatabase(); }
public OrganizationController(IUoWData db) { this.db = db; this.mongoDb = MongoClientFactory.GetDatabase(); }
static HistoryController() { mongoDb = MongoClientFactory.GetDatabase(); historyCollection = mongoDb.GetCollection <HistoryEntry>(MongoCollections.History); }