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
465 changes: 465 additions & 0 deletions backend/api/internal/database/comment_queries.go

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions backend/api/internal/database/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ CREATE TABLE Projects (
CREATE TABLE ProjectComments (
project_id INTEGER NOT NULL,
comment_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES Users(id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES Projects(id) ON DELETE CASCADE,
FOREIGN KEY (comment_id) REFERENCES Comments(id) ON DELETE CASCADE,
PRIMARY KEY (project_id, comment_id)
Expand All @@ -72,6 +74,8 @@ CREATE TABLE Posts (
CREATE TABLE PostComments (
post_id INTEGER NOT NULL,
comment_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES Users(id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES Posts(id) ON DELETE CASCADE,
FOREIGN KEY (comment_id) REFERENCES Comments(id) ON DELETE CASCADE,
PRIMARY KEY (post_id, comment_id)
Expand All @@ -81,11 +85,10 @@ CREATE TABLE PostComments (
CREATE TABLE Comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
post_id INTEGER NOT NULL,
parent_comment_id INTEGER,
likes INTEGER NOT NULL,
creation_date TIMESTAMP NOT NULL,
user_id INTEGER NOT NULL,
FOREIGN KEY (post_id) REFERENCES Posts(id) ON DELETE CASCADE,
FOREIGN KEY (parent_comment_id) REFERENCES Comments(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES Users(id) ON DELETE CASCADE
);
Expand Down
84 changes: 43 additions & 41 deletions backend/api/internal/database/create_test_data.sql
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
-- Insert Users with fixed creation dates
-- Users
INSERT INTO Users (username, picture, bio, links, creation_date) VALUES
('dev_user1', 'https://example.com/dev_user1.jpg', 'Full-stack developer passionate about open-source projects.', '["https://github.com/dev_user1", "https://devuser1.com"]', '2023-12-13 00:00:00'),
('tech_writer2', 'https://example.com/tech_writer2.jpg', 'Technical writer and Python enthusiast.', '["https://blog.techwriter.com"]', '2022-12-13 00:00:00'),
('data_scientist3', 'https://example.com/data_scientist3.jpg', 'Data scientist with a passion for machine learning.', '["https://github.com/data_scientist3", "https://datascientist3.com"]', '2023-06-13 00:00:00');
('data_scientist3', 'https://example.com/data_scientist3.jpg', 'Data scientist with a passion for machine learning.', '["https://github.com/data_scientist3", "https://datascientist3.com"]', '2023-06-13 00:00:00'),
('backend_guru4', 'https://example.com/backend_guru4.jpg', 'Backend expert specializing in scalable systems.', '["https://github.com/backend_guru4"]', '2024-01-15 00:00:00'),
('ui_designer5', 'https://example.com/ui_designer5.jpg', 'UI/UX designer with a love for user-friendly apps.', '["https://portfolio.uidesigner5.com"]', '2023-05-10 00:00:00');

-- Insert Projects with fixed creation dates
-- Projects
INSERT INTO Projects (name, description, status, likes, tags, links, owner, creation_date) VALUES
('OpenAPI Toolkit', 'A toolkit for generating and testing OpenAPI specs.', 1, 120, '["OpenAPI", "Go", "Tooling"]', '["https://github.com/dev_user1/openapi-toolkit"]', (SELECT id FROM Users WHERE username = 'dev_user1'), '2023-06-13 00:00:00'),
('DocuHelper', 'A library for streamlining technical documentation processes.', 2, 85, '["Documentation", "Python"]', '["https://github.com/tech_writer2/docuhelper"]', (SELECT id FROM Users WHERE username = 'tech_writer2'), '2021-12-13 00:00:00'),
('ML Research', 'Research repository for various machine learning algorithms.', 1, 45, '["Machine Learning", "Python", "Research"]', '["https://github.com/data_scientist3/ml-research"]', (SELECT id FROM Users WHERE username = 'data_scientist3'), '2024-09-13 00:00:00');
('ML Research', 'Research repository for various machine learning algorithms.', 1, 45, '["Machine Learning", "Python", "Research"]', '["https://github.com/data_scientist3/ml-research"]', (SELECT id FROM Users WHERE username = 'data_scientist3'), '2024-09-13 00:00:00'),
('ScaleDB', 'A scalable database system for modern apps.', 1, 70, '["Database", "Scalability", "Backend"]', '["https://github.com/backend_guru4/scaledb"]', (SELECT id FROM Users WHERE username = 'backend_guru4'), '2024-03-15 00:00:00');

-- Insert Posts with fixed creation dates
-- Posts
INSERT INTO Posts (content, project_id, creation_date, user_id, likes) VALUES
('Excited to release the first version of OpenAPI Toolkit!', (SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), '2024-09-13 00:00:00', (SELECT id FROM Users WHERE username = 'dev_user1'), 40),
('We''ve archived DocuHelper, but feel free to explore the code.', (SELECT id FROM Projects WHERE name = 'DocuHelper'), '2024-06-13 00:00:00', (SELECT id FROM Users WHERE username = 'tech_writer2'), 25),
('Updated ML Research repo with new algorithms for data analysis.', (SELECT id FROM Projects WHERE name = 'ML Research'), '2024-11-13 00:00:00', (SELECT id FROM Users WHERE username = 'data_scientist3'), 15);

-- Insert Comments with fixed creation dates
INSERT INTO Comments (content, post_id, parent_comment_id, creation_date, user_id) VALUES
('This is amazing! Can''t wait to try it out.', (SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), NULL, '2024-10-13 00:00:00', (SELECT id FROM Users WHERE username = 'tech_writer2')),
('Thanks for the kind words!', (SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'This is amazing! Can''t wait to try it out.'), '2024-11-13 00:00:00', (SELECT id FROM Users WHERE username = 'dev_user1')),
('Looks great! I''ll test it and report back.', (SELECT id FROM Posts WHERE content = 'Updated ML Research repo with new algorithms for data analysis.'), NULL, '2024-11-13 00:00:00', (SELECT id FROM Users WHERE username = 'data_scientist3'));

-- Insert Project Likes with fixed dates
INSERT INTO ProjectLikes (project_id, user_id) VALUES
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Users WHERE username = 'dev_user1')),
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Projects WHERE name = 'DocuHelper'), (SELECT id FROM Users WHERE username = 'dev_user1')),
((SELECT id FROM Projects WHERE name = 'ML Research'), (SELECT id FROM Users WHERE username = 'data_scientist3'));

-- Insert Post Likes with fixed dates
INSERT INTO PostLikes (post_id, user_id) VALUES
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Users WHERE username = 'dev_user1')),
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Posts WHERE content = 'We''ve archived DocuHelper, but feel free to explore the code.'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Posts WHERE content = 'Updated ML Research repo with new algorithms for data analysis.'), (SELECT id FROM Users WHERE username = 'data_scientist3'));

-- Insert Comment Likes with fixed dates
INSERT INTO CommentLikes (comment_id, user_id) VALUES
((SELECT id FROM Comments WHERE content = 'This is amazing! Can''t wait to try it out.'), (SELECT id FROM Users WHERE username = 'dev_user1')),
((SELECT id FROM Comments WHERE content = 'Thanks for the kind words!'), (SELECT id FROM Users WHERE username = 'tech_writer2'));

-- Insert User Follows with fixed dates
INSERT INTO UserFollows (follower_id, follows_id) VALUES
((SELECT id FROM Users WHERE username = 'dev_user1'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Users WHERE username = 'tech_writer2'), (SELECT id FROM Users WHERE username = 'data_scientist3')),
((SELECT id FROM Users WHERE username = 'dev_user1'), (SELECT id FROM Users WHERE username = 'data_scientist3')),
((SELECT id FROM Users WHERE username = 'data_scientist3'), (SELECT id FROM Users WHERE username = 'dev_user1'));

-- Insert Project Follows with fixed dates
INSERT INTO ProjectFollows (project_id, user_id) VALUES
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Projects WHERE name = 'ML Research'), (SELECT id FROM Users WHERE username = 'dev_user1'));
-- Comments on Projects (Parent-child relationships with hardcoded parent_comment_id)
INSERT INTO Comments (content, parent_comment_id, likes, creation_date, user_id) VALUES
('This is a fantastic project! Can''t wait to contribute.', NULL, 5, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'dev_user1')),
('I love the concept, but I think the documentation could be improved.', NULL, 3, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'tech_writer2')),
('Great to see more open-source tools for API development!', NULL, 4, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'backend_guru4')),
('I agree, but the API specs seem a bit too complex for beginners.', 3, 2, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'data_scientist3')), -- Hardcoded parent_comment_id = 3
('I hope this toolkit will integrate with other Go tools soon!', 1, 1, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'ui_designer5')), -- Hardcoded parent_comment_id = 1
('I agree, the documentation is lacking in detail.', 2, 1, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'data_scientist3')); -- Hardcoded parent_comment_id = 2

-- ProjectComments relations (Mapping comments to projects)
INSERT INTO ProjectComments (project_id, comment_id, user_id) VALUES
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Comments WHERE content = 'This is a fantastic project! Can''t wait to contribute.'), (SELECT id FROM Users WHERE username = 'dev_user1')),
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Comments WHERE content = 'I love the concept, but I think the documentation could be improved.'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Comments WHERE content = 'Great to see more open-source tools for API development!'), (SELECT id FROM Users WHERE username = 'backend_guru4')),
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Comments WHERE content = 'I agree, but the API specs seem a bit too complex for beginners.'), (SELECT id FROM Users WHERE username = 'data_scientist3')),
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Comments WHERE content = 'I hope this toolkit will integrate with other Go tools soon!'), (SELECT id FROM Users WHERE username = 'ui_designer5')),
((SELECT id FROM Projects WHERE name = 'OpenAPI Toolkit'), (SELECT id FROM Comments WHERE content = 'I agree, the documentation is lacking in detail.'), (SELECT id FROM Users WHERE username = 'data_scientist3'));

-- Comments on Posts (Parent-child relationships with hardcoded parent_comment_id)
INSERT INTO Comments (content, parent_comment_id, likes, creation_date, user_id) VALUES
('Awesome update! I''ll try it out.', NULL, 2, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'backend_guru4')),
('Thanks for sharing! Will this feature be extended soon?', NULL, 1, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'data_scientist3')),
('Great work, looking forward to more updates!', NULL, 4, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'ui_designer5')),
('Will this be compatible with earlier versions of OpenAPI?', 2, 1, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'tech_writer2')), -- Hardcoded parent_comment_id = 2
('I hope the next update addresses performance improvements.', 1, 3, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'data_scientist3')), -- Hardcoded parent_comment_id = 1
('Looking forward to testing it!', 3, 2, '2024-12-23 00:00:00', (SELECT id FROM Users WHERE username = 'dev_user1')); -- Hardcoded parent_comment_id = 3

-- PostComments relations (Mapping comments to posts)
INSERT INTO PostComments (post_id, comment_id, user_id) VALUES
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'Awesome update! I''ll try it out.'), (SELECT id FROM Users WHERE username = 'backend_guru4')),
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'Thanks for sharing! Will this feature be extended soon?'), (SELECT id FROM Users WHERE username = 'data_scientist3')),
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'Great work, looking forward to more updates!'), (SELECT id FROM Users WHERE username = 'ui_designer5')),
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'Will this be compatible with earlier versions of OpenAPI?'), (SELECT id FROM Users WHERE username = 'tech_writer2')),
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'I hope the next update addresses performance improvements.'), (SELECT id FROM Users WHERE username = 'data_scientist3')),
((SELECT id FROM Posts WHERE content = 'Excited to release the first version of OpenAPI Toolkit!'), (SELECT id FROM Comments WHERE content = 'Looking forward to testing it!'), (SELECT id FROM Users WHERE username = 'dev_user1'));
Binary file modified backend/api/internal/database/dev.sqlite3
Binary file not shown.
4 changes: 2 additions & 2 deletions backend/api/internal/database/post_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func QueryUpdatePost(id int, updatedData map[string]interface{}) error {
// - id: The unique identifier of the user to query.
//
// Returns:
// - *types.Post: The post details if found.
// - []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 = ?;`
Expand All @@ -140,7 +140,7 @@ func QueryPostsByUserId(userId int) ([]types.Post, int, error) {
}
defer rows.Close()

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

for rows.Next() {
var post types.Post
Expand Down
49 changes: 49 additions & 0 deletions backend/api/internal/database/project_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,55 @@ func QueryProject(id int) (*types.Project, error) {
return &project, nil
}

// QueryProjectsByUserId retrieves a user's projects by a user ID from the database.
//
// Parameters:
// - id: The unique identifier of the user to query projects on.
//
// Returns:
// - *[]types.Project: A list of the projects' details if found.
// - error: An error if the query fails. Returns nil for both if no project exists.
func QueryProjectsByUserId(userId int) ([]types.Project, int, error) {
query := `SELECT id, name, description, status, likes, links, tags, owner, creation_date FROM Projects WHERE owner = ?;`
rows, err := DB.Query(query, userId)
if err != nil {
return nil, http.StatusNotFound, err
}
var projects []types.Project
defer rows.Close()

for rows.Next() {
var project types.Project
var linksJSON, tagsJSON string
err := rows.Scan(
&project.ID,
&project.Name,
&project.Description,
&project.Status,
&project.Likes,
&linksJSON,
&tagsJSON,
&project.Owner,
&project.CreationDate,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, http.StatusOK, nil
}
return nil, http.StatusInternalServerError, err
}

if err := UnmarshalFromJSON(linksJSON, &project.Links); err != nil {
return nil, http.StatusBadRequest, err
}
if err := UnmarshalFromJSON(tagsJSON, &project.Tags); err != nil {
return nil, http.StatusBadRequest, err
}
}

return projects, http.StatusOK, nil
}

// QueryCreateProject creates a new project in the database.
//
// Parameters:
Expand Down
Loading