Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified backend/api/internal/database/dev.sqlite3
Binary file not shown.
92 changes: 88 additions & 4 deletions backend/api/internal/database/post_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package database
import (
"database/sql"
"fmt"
"net/http"
"time"

"backend/api/internal/types"
)

// QueryProject retrieves a project by its ID from the database.
// QueryPosts retrieves a post by its ID from the database.
//
// Parameters:
// - id: The unique identifier of the project to query.
// - id: The unique identifier of the post to query.
//
// Returns:
// - *types.Project: The project details if found.
// - error: An error if the query fails. Returns nil for both if no project exists.
// - *types.Post: The post details if found.
// - error: An error if the query fails. Returns nil for both if no post exists.
func QueryPost(id int) (*types.Post, error) {
query := `SELECT id, user_id, project_id, content, likes, creation_date FROM Posts WHERE id = ?;`
row := DB.QueryRow(query, id)
Expand Down Expand Up @@ -121,3 +122,86 @@ func QueryUpdatePost(id int, updatedData map[string]interface{}) error {

return nil
}

// QueryPostsByUserId retrieves a set of posts by its owning user id from the database.
//
// Parameters:
// - id: The unique identifier of the user to query.
//
// Returns:
// - *types.Post: The post details if found.
// - error: An error if the query fails. Returns nil for both if no post exists.
func QueryPostsByUserId(userId int) ([]types.Post, int, error) {
query := `SELECT id, user_id, project_id, content, likes, creation_date FROM Posts WHERE user_id = ?;`

rows, err := DB.Query(query, userId)
if err != nil {
return nil, http.StatusNotFound, err
}
defer rows.Close()

var posts []types.Post = []types.Post{}

for rows.Next() {
var post types.Post
err := rows.Scan(
&post.ID,
&post.User,
&post.Project,
&post.Content,
&post.Likes,
&post.CreationDate,
)

if err != nil {
if err == sql.ErrNoRows {
return []types.Post{}, http.StatusOK, nil
}
return nil, http.StatusInternalServerError, err
}
posts = append(posts, post)
}

return posts, http.StatusOK, nil
}

// QueryPostsByProjectId retrieves a set of posts by its owning project id from the database.
//
// Parameters:
// - id: The unique identifier of the project to query.
//
// Returns:
// - *types.Post: The post details if found.
// - error: An error if the query fails. Returns nil for both if no post exists.
func QueryPostsByProjectId(projId int) ([]types.Post, int, error) {
query := `SELECT id, user_id, project_id, content, likes, creation_date FROM Posts WHERE project_id = ?;`

rows, err := DB.Query(query, projId)
if err != nil {
return nil, http.StatusNotFound, err
}
defer rows.Close()

var posts []types.Post = []types.Post{}

for rows.Next() {
var post types.Post
err := rows.Scan(
&post.ID,
&post.User,
&post.Project,
&post.Content,
&post.Likes,
&post.CreationDate,
)

if err != nil {
if err == sql.ErrNoRows {
return []types.Post{}, http.StatusOK, nil
}
return nil, http.StatusInternalServerError, err
}
posts = append(posts, post)
}
return posts, http.StatusOK, nil
}
4 changes: 2 additions & 2 deletions backend/api/internal/database/project_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func QueryGetProjectFollowingNames(username string) ([]string, int, error) {
// - int: HTTP-like status code indicating the result of the operation.
// - error: An error if the query fails.
func getProjectFollowersOrFollowing(query string, userID int) ([]int, int, error) {
rows, err := ExecQuery(query, userID)
rows, err := DB.Query(query, userID)
if err != nil {
return nil, http.StatusNotFound, err
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func getProjectFollowersOrFollowing(query string, userID int) ([]int, int, error
// - int: HTTP-like status code indicating the result of the operation.
// - error: An error if the query fails.
func getProjectFollowersOrFollowingUsernames(query string, projectID int) ([]string, int, error) {
rows, err := ExecQuery(query, projectID)
rows, err := DB.Query(query, projectID)
if err != nil {
return nil, http.StatusNotFound, err
}
Expand Down
64 changes: 32 additions & 32 deletions backend/api/internal/database/user_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ func QueryGetUsersFollowersUsernames(username string) ([]string, int, error) {
// function to retrieve the user ids of the users who follow the given user
//
// Parameters:
// - username (string): the user to retrieve
// - username (string): the user to retrieve
//
// Returns:
// - []int: a list of user ids of users who follow the specified user
// - int: HTTP status code indicating the result of the operation
// - error: any error encountered during the query
// - []int: a list of user ids of users who follow the specified user
// - int: HTTP status code indicating the result of the operation
// - error: any error encountered during the query
func QueryGetUsersFollowers(username string) ([]int, int, error) {
userID, err := GetUserIdByUsername(username)
if err != nil {
Expand All @@ -235,12 +235,12 @@ func QueryGetUsersFollowers(username string) ([]int, int, error) {
// function to retrieve the usernames of the users who follow the given user
//
// Parameters:
// - username (string): the user to retrieve
// - username (string): the user to retrieve
//
// Returns:
// - []string: a list of usernames of users who follow the specified user
// - int: HTTP status code indicating the result of the operation
// - error: any error encountered during the query
// - []string: a list of usernames of users who follow the specified user
// - int: HTTP status code indicating the result of the operation
// - error: any error encountered during the query
func QueryGetUsersFollowingUsernames(username string) ([]string, int, error) {
userID, err := GetUserIdByUsername(username)
if err != nil {
Expand All @@ -259,12 +259,12 @@ func QueryGetUsersFollowingUsernames(username string) ([]string, int, error) {
// function to retrieve the ids of the users who follow the given user
//
// Parameters:
// - username (string) - the user to retrieve
// - username (string) - the user to retrieve
//
// Returns:
// - []int: a list of user IDs of users who follow the specified user
// - int: HTTP status code indicating the result of the operation
// - error: any error encountered during the query
// - []int: a list of user IDs of users who follow the specified user
// - int: HTTP status code indicating the result of the operation
// - error: any error encountered during the query
func QueryGetUsersFollowing(username string) ([]int, int, error) {
userID, err := GetUserIdByUsername(username)
if err != nil {
Expand All @@ -283,15 +283,15 @@ func QueryGetUsersFollowing(username string) ([]int, int, error) {
// helper function to retrieve the followers or followings of a user by their IDs
//
// Parameters:
// - query (string): the SQL query to execute
// - userID (int): the ID of the user to find follow data for
// - query (string): the SQL query to execute
// - userID (int): the ID of the user to find follow data for
//
// Returns:
// - []int: a list of user IDs for the followers or followings
// - int: HTTP status code
// - error: any error encountered during the query
// - []int: a list of user IDs for the followers or followings
// - int: HTTP status code
// - error: any error encountered during the query
func getUsersFollowingOrFollowers(query string, userID int) ([]int, int, error) {
rows, err := ExecQuery(query, userID)
rows, err := DB.Query(query, userID)
if err != nil {
return nil, http.StatusNotFound, err
}
Expand All @@ -316,15 +316,15 @@ func getUsersFollowingOrFollowers(query string, userID int) ([]int, int, error)
// helper function to retrieve the followers or followings of a user by their usernames
//
// Parameters:
// - query (string): the SQL query to execute
// - userID (int): the ID of the user to find follow data for
// - query (string): the SQL query to execute
// - userID (int): the ID of the user to find follow data for
//
// Returns:
// - []string: a list of usernames for the followers or followings
// - int: HTTP status code
// - error: any error encountered during the query
// - []string: a list of usernames for the followers or followings
// - int: HTTP status code
// - error: any error encountered during the query
func getUsersFollowingOrFollowersUsernames(query string, userID int) ([]string, int, error) {
rows, err := ExecQuery(query, userID)
rows, err := DB.Query(query, userID)
if err != nil {
return nil, http.StatusNotFound, err
}
Expand All @@ -349,12 +349,12 @@ func getUsersFollowingOrFollowersUsernames(query string, userID int) ([]string,
// function to create a follow relationship between two users
//
// Parameters:
// - user (string): the username of the user initiating the follow
// - newFollow (string): the username of the user to be followed
// - user (string): the username of the user initiating the follow
// - newFollow (string): the username of the user to be followed
//
// Returns:
// - int: HTTP status code
// - error: any error encountered during the query
// - int: HTTP status code
// - error: any error encountered during the query
func CreateNewUserFollow(user string, newFollow string) (int, error) {
userID, err := GetUserIdByUsername(user)
if err != nil {
Expand Down Expand Up @@ -391,12 +391,12 @@ func CreateNewUserFollow(user string, newFollow string) (int, error) {
// function to remove a follow relationship between two users
//
// Parameters:
// - user (string): the username of the user initiating the unfollow
// - unfollow (string): the username of the user to be unfollowed
// - user (string): the username of the user initiating the unfollow
// - unfollow (string): the username of the user to be unfollowed
//
// Returns:
// - int: HTTP status code
// - error: any error encountered during the query
// - int: HTTP status code
// - error: any error encountered during the query
func RemoveUserFollow(user string, unfollow string) (int, error) {
userID, err := GetUserIdByUsername(user)
if err != nil {
Expand Down
44 changes: 11 additions & 33 deletions backend/api/internal/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
package database

import (
"database/sql"
"encoding/json"
"fmt"

"backend/api/internal/logger"
)


// takes in some sort of data, and changes it to a JSON
// data type. Will return an error if it is not JSON-esque data
//
// input:
// input:
// interface (interface{}) - the data to be converted to JSON
// output:
// output:
// the JSON string
// an error

Expand All @@ -34,14 +32,13 @@ func MarshalToJSON(value interface{}) (string, error) {
return string(linksJSON), nil
}


// takes in some JSON data, and changes it to a JSON
// data type. Will return an error if it is not JSON-esque data
//
// input:
// input:
// data (string) - the data in a string, edited in place
// interface (interface{}) - the data to be put into the string
// output:
// output:
// error

func UnmarshalFromJSON(data string, target interface{}) error {
Expand All @@ -53,28 +50,6 @@ func UnmarshalFromJSON(data string, target interface{}) error {
return nil
}


// takes in a query and some params to add to it
// and will execute the query, given the database is
// setup and connected.
//
// input:
// query (string) - the base query string
// args (...interface{}) - the params of the query
// output:
// *sql.Rows - the resulting rows
// error

func ExecQuery(query string, args ...interface{}) (*sql.Rows, error) {
rows, err := DB.Query(query, args...)
if err != nil {
logger.Log.Errorf("Error executing query: %v", err)
return nil, fmt.Errorf("Error executing query: %v", err)
}
return rows, nil
}


// takes in a query and some params to add to it
// and will execute the update query, given the database is
// setup and connected.
Expand Down Expand Up @@ -105,11 +80,14 @@ func ExecUpdate(query string, args ...interface{}) (int64, error) {
// and prepares the corresponding arguments, including marshaling JSON data for special fields (like links and tags).
//
// input:
// updatedData (map[string]interface{}) - a JSON like structure with all of the updatedData for the query
//
// updatedData (map[string]interface{}) - a JSON like structure with all of the updatedData for the query
//
// output:
// string - the partially completed query, with all of the fields added
// []interface{} - the arguments for the query
// the error
//
// string - the partially completed query, with all of the fields added
// []interface{} - the arguments for the query
// the error
func BuildUpdateQuery(updatedData map[string]interface{}) (string, []interface{}, error) {
var query string
var args []interface{}
Expand Down
Loading