public ActionResult Chatroom(string inputMessage) { NotGitterDBEntities db = new NotGitterDBEntities(); List <Message> messages = new List <Message>(); string repoName = ""; //inputMessage is empty if (inputMessage.Equals("")) { messages = getMessage(); return(View(messages)); } // Get CurrentTime DateTime currentTime = DateTime.Now; //Get Repo Name from url parameter if (Request.QueryString["repoName"] != null) { repoName = Request.QueryString["repoName"]; } //Get current user int userId = Convert.ToInt32(Session["userId"]); //Get All Repo made by userId ICollection <Repo> allRepo = db.Repoes.Where(rp => rp.UId == userId).ToList(); Repo repo = new Repo(); // Loop through All repo made by userid, find repo name that are corresponding repo chatroom name foreach (Repo rp in allRepo) { if (rp.name.Equals(repoName)) { repo = rp; continue; } } //Create Message object for new message Message newMessage = new Message(); //Add information of new Message newMessage.Content = inputMessage; newMessage.Uid = userId; newMessage.timestamp = currentTime; newMessage.RepoId = repo.RepoId; //Add to Database db.Messages.Add(newMessage); //save changes db.SaveChanges(); //Getting all message for listing messages = getMessage(); //Return to chatroom view with list of messages return(View(messages)); }
//Helper function to return list of messages to display public List <Message> getMessage() { NotGitterDBEntities db = new NotGitterDBEntities(); string repoName = ""; //Get Repo Name from url parameter if (Request.QueryString["repoName"] != null) { repoName = Request.QueryString["repoName"]; } Repo repo = new Repo(); ICollection <Repo> repoes = db.Repoes.Where(rp => rp.name == repoName).ToList(); List <long> repoid = new List <long>(); List <Message> messages = new List <Message>(); // Get All the Repo ID that have same Repo name foreach (Repo rp in repoes) { repoid.Add(rp.RepoId); } // Get all the Message that contain repoid for (int i = 0; i < repoid.Count(); i++) { long actualRepoId = repoid.ElementAt(i); ICollection <Message> oneRepoMessages = db.Messages.Where(m => m.RepoId == actualRepoId).ToList(); foreach (Message m in oneRepoMessages) { messages.Add(m); } } //swap message according to timestamp var message = from m in messages orderby m.timestamp select m; return(message.ToList()); }
public async Task <ActionResult> Index() { var accessToken = Session["OAuthToken"] as string; if (accessToken != null) { // This allows the client to make requests to the GitHub API on the user's behalf // without ever having the user's OAuth credentials. client.client.Credentials = new Credentials(accessToken); } else { return(Redirect(GetOauthLoginUrl())); } try { // The following requests retrieves all of the user's repositories and // requires that the user be logged in to work. //add repos and user to DB var userDetails = await client.client.User.Current(); Models.User currentUser = new Models.User(); NotGitterDBEntities dbContextRef = new NotGitterDBEntities(); var emailList = await client.client.User.Email.GetAll(); currentUser.email = emailList.ToArray()[0].Email; currentUser.name = userDetails.Login; currentUser.online = 1; var currentUserId = 0; var tempGithubId = await client.client.User.Current(); currentUser.GithubId = tempGithubId.Id; //write your id here Models.User checkuser = dbContextRef.Users.Where(d => d.GithubId == currentUser.GithubId).FirstOrDefault(); if (checkuser == null) { dbContextRef.Users.Add(currentUser); currentUserId = currentUser.UId; } else { dbContextRef.Entry(checkuser).State = EntityState.Modified; currentUserId = checkuser.UId; } IReadOnlyList <Repository> repos = await client.client.Repository.GetAllForCurrent(); foreach (Repository e in repos) { Models.Repo oldone = await dbContextRef.Repoes.Where(rp => rp.url == e.HtmlUrl).FirstOrDefaultAsync <Models.Repo>(); Models.Repo newone1 = new Models.Repo(); if (oldone != null) { oldone.UId = currentUserId; oldone.url = e.HtmlUrl; oldone.dateCreated = e.CreatedAt.DateTime; oldone.language = e.Language; oldone.C_private_ = e.Private ? 1 : 0; oldone.RepoId = Convert.ToInt32(e.Id); oldone.name = e.Name; oldone.Stars = e.StargazersCount; oldone.Description = e.Description; dbContextRef.Entry(oldone).State = EntityState.Modified; } else { newone1.UId = currentUserId; newone1.url = e.HtmlUrl; newone1.dateCreated = e.CreatedAt.DateTime; newone1.language = e.Language; newone1.C_private_ = e.Private ? 1 : 0; newone1.RepoId = Convert.ToInt32(e.Id); newone1.name = e.Name; newone1.Description = e.Description; newone1.Stars = e.StargazersCount; dbContextRef.Repoes.Add(newone1); } try { dbContextRef.SaveChanges(); } catch (Exception ex) { continue; } } //d.Result.ToArray()[0].Email; //then add the repos if they don't exist //note: at this point you'd have to create a chat room for each repo that doesn't exist //index should show a list of all the repos(chat rooms available) // don't forget to get the id of the owner // TODO: make a viewModel for this mess // TODO: the view for this page should NOT show the chat // index(list of repos)->(repos' chat panel)->(repo's details) ViewBag.name = currentUser.name; Session["userId"] = currentUserId; Session["userName"] = currentUser.name; return(View(dbContextRef.Repoes.Where(e => e.UId == currentUserId).ToList())); } catch (AuthorizationException) { // Either the accessToken is null or it's invalid. This redirects // to the GitHub OAuth login page. That page will redirect back to the // Authorize action. return(Redirect(GetOauthLoginUrl())); } }