Skip to content

Commit

Permalink
Merge pull request #73 from ContainerMaintainers/development
Browse files Browse the repository at this point in the history
New release, v1.2.0
  • Loading branch information
adamjhr authored Mar 11, 2023
2 parents 29f1dcd + d5fc82c commit b4dd6c8
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 171 deletions.
167 changes: 130 additions & 37 deletions controllers/tweets_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,147 @@ import (

const Per_page int = 30

type JoinedMessage struct {
Author_id uint
Username string
Text string
Pub_Date uint
}

func GetMessages(timelineType string, user int) []JoinedMessage {

var joinedMessages []JoinedMessage

if timelineType == "public" {
// Join messages and users tables for public timeline
database.DB.Table("messages").
Select("messages.Author_id", "users.Username" ,"messages.Text", "messages.Pub_Date").
Joins("left join users on users.id = messages.Author_id").Scan(&joinedMessages)

} else if timelineType == "myTimeline" {
// Join messages, users, and followers for my timeline
database.DB.Table("messages").
Select("messages.Author_id", "users.Username" ,"messages.Text", "messages.Pub_Date").
Joins("left join followers on messages.author_id = followers.whom_id").
Joins("left join users on users.id = messages.Author_id").
Where("messages.flagged = ? AND (messages.author_id = ? OR followers.who_id = ?)", false, user, user).
Scan(&joinedMessages)
} else if timelineType == "individual"{
// Join messages and users for an individual's timeline
database.DB.Table("messages").
Select("messages.Author_id", "users.Username" ,"messages.Text", "messages.Pub_Date").
Joins("left join users on users.id = messages.Author_id").
Where("messages.flagged = ? AND (messages.author_id = ?)", false, user).
Scan(&joinedMessages)
}

return joinedMessages

}

// ENDPOINT: GET /ping
func ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}

// ENDPOINT: GET /public
func public(c *gin.Context) { //Displays the latest messages of all users

var messages []entities.Message

if err := database.DB.Where("Flagged = false").Order("Pub_Date desc").Limit(Per_page).Find(&messages).Error; err != nil {
log.Print("Ran into error during " + c.Request.RequestURI + ": " + err.Error())
c.AbortWithStatus(400)
return
// ENDPOINT: GET /
func timeline(c *gin.Context) {

// if there is NO session user, show public timeline
if user == -1 {
c.HTML(http.StatusOK, "timeline.html", gin.H{
"messages": GetMessages("public", user),
})
} else {
// if there exists a session user, show my timeline
c.HTML(http.StatusOK, "timeline.html", gin.H{
"messages": GetMessages("myTimeline", user),
"user": user,
})
}

c.HTML(http.StatusOK, "timeline.html", gin.H{
"messages": messages,
})
}

// ENDPOINT: GET /
func timeline(c *gin.Context) {
// ENDPOINT: GET /public
func public(c *gin.Context) {

// check if there exists a session user, if not, return all messages
// For now just reuse the same endpoint handler as /public
if user == -1 {
public(c)
return
c.HTML(http.StatusOK, "timeline.html", gin.H{
"messages": GetMessages("public", user),
})
} else {
// if there exists a session user, show my timeline
c.HTML(http.StatusOK, "timeline.html", gin.H{
"messages": GetMessages("public", user),
"user": user,
})
}
}

var messages []entities.Message
// ENDPOINT: GET /:username
func username(c *gin.Context) { // Displays an individual's timeline

if err := database.DB.Table("messages").
Joins("left join followers on messages.author_id = followers.whom_id").
Where("messages.flagged = ? AND (messages.author_id = ? OR followers.who_id = ?)", false, user, user).
Limit(Per_page).Find(&messages).Error; err != nil { // ORDER BY DATE
log.Print("Ran into error during " + c.Request.RequestURI + ": " + err.Error())
username := c.Param("username") // gets the <username> from the url
userID, err := getUserId(username)
if err != nil {
log.Print("Bad request during " + c.Request.RequestURI + ": " + " User " + username + " not found")
c.Status(404)
return
}

c.HTML(http.StatusOK, "timeline.html", gin.H{
"messages": messages,
})
// if endpoint is a username
if username != "" {
// if logged in
if user != -1 {
followed := GetFollower(uint(userID), uint(user))
var users_page = false
// If logged in user == endpoint
if user == int(userID) {
users_page = true

c.HTML(http.StatusOK, "timeline.html", gin.H{
"title": "My Timeline ONE",
"user": user,
"private": true,
"user_page": true,
"messages": GetMessages("myTimeline", user),
})
} else {
// If following
if followed == true {
// If logged in and user != endpoint
c.HTML(http.StatusOK, "timeline.html", gin.H{
"title": username + "'s Timeline TWO",
"user_timeline": true,
"private": true,
"user": username,
"followed": followed,
"user_page": users_page,
"messages": GetMessages("individual", int(userID)),
})
} else {
// If not following
// If logged in and user != endpoint
c.HTML(http.StatusOK, "timeline.html", gin.H{
"title": username + "'s Timeline THREE",
"user_timeline": true,
"private": true,
"user": username,
"user_page": users_page,
"messages": GetMessages("individual", int(userID)),
})
}
}
} else {
// If not logged in
c.HTML(http.StatusOK, "timeline.html", gin.H{
"title": username + "'s Timeline FOUR",
"user_timeline": true,
"private": true,
"messages": GetMessages("individual", int(userID)),
})
}
}
}

// ENDPOINT: POST /add_message
Expand All @@ -74,7 +170,7 @@ func addMessage(c *gin.Context) { //Registers a new message for the user.
c.Bind(&body)

message := entities.Message{
Author_id: uint(user), // AUTHOR ID SHOULD GET SESSION USER ID
Author_id: uint(user), // GET SESSION USER ID
Text: body.Text,
Pub_Date: uint(time.Now().Unix()),
Flagged: false,
Expand All @@ -87,10 +183,7 @@ func addMessage(c *gin.Context) { //Registers a new message for the user.
return
}

//redirect to timeline ("/")
//c.Redirect(200, "/") // For some reason, this returns error 500, but I assume it's because the path doesn't exist yet?
// Temporarily dont redirect
c.String(200, "Your message was recorded")
c.Redirect(http.StatusFound, "/")

}

Expand All @@ -104,14 +197,14 @@ func SetupRouter() *gin.Engine {
router.GET("/", timeline)
router.GET("/public", public)
router.GET("/:username", username)
router.POST("/:username/follow", usernameFollow)
router.DELETE("/:username/unfollow", usernameUnfollow)
router.GET("/:username/follow", usernameFollow)
router.GET("/:username/unfollow", usernameUnfollow)
router.POST("/register", register_user)
router.GET("/register", register)
router.POST("/add_message", addMessage)
router.POST("/login", login_user)
router.GET("/login", loginf)
router.PUT("/logout", logoutf) // Changed temporarily to satisfy tests, should it be put or get?
router.GET("/logout", logout_user)

router.GET("/sim/latest", simLatest)
router.POST("/sim/register", simRegister)
Expand Down
Loading

0 comments on commit b4dd6c8

Please sign in to comment.