Skip to content

Commit

Permalink
Added Categories and CategoriesAssignmentsTable
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Singh <[email protected]>
  • Loading branch information
SkySingh04 committed Jul 12, 2024
1 parent 65dc77a commit bca72b2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions auth-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func main() {
db.CreateUsersTable()
db.CreateBlogPostsTable()
db.CreateCommentsTable()
db.CreateCategoriesTable()
db.CreateCategoryAssignmentsTable()
r := gin.Default()
err := godotenv.Load()

Expand Down
2 changes: 2 additions & 0 deletions blog-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func main() {
db.CreateUsersTable()
db.CreateBlogPostsTable()
db.CreateCommentsTable()
db.CreateCategoriesTable()
db.CreateCategoryAssignmentsTable()
err := godotenv.Load()

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions comment-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func main() {
defer db.CloseDB()

db.CreateCommentsTable()
db.CreateCategoriesTable()
db.CreateCategoryAssignmentsTable()
db.CreateBlogPostsTable()
db.CreateUsersTable()
r := gin.Default()
Expand Down
37 changes: 37 additions & 0 deletions db/createtables.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,40 @@ func CreateCommentsTable() {

log.Println("comments table created successfully")
}

func CreateCategoriesTable() {
// SQL statement to create the categories table
const createTableQuery = `
CREATE TABLE IF NOT EXISTS categories (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
`

// Execute the SQL statement to create the table
_, err := db.Exec(createTableQuery)
if err != nil {
log.Fatalf("Error creating categories table: %v", err)
}

log.Println("categories table created successfully")
}

func CreateCategoryAssignmentsTable() {
// SQL statement to create the category_assignments table (weak entity)
const createTableQuery = `
CREATE TABLE IF NOT EXISTS category_assignments (
id SERIAL PRIMARY KEY,
category_id INTEGER NOT NULL REFERENCES categories(id) ON DELETE CASCADE,
blog_id INTEGER NOT NULL REFERENCES blog_posts(id) ON DELETE CASCADE
);
`

// Execute the SQL statement to create the table
_, err := db.Exec(createTableQuery)
if err != nil {
log.Fatalf("Error creating category_assignments table: %v", err)
}

log.Println("category_assignments table created successfully")
}
2 changes: 2 additions & 0 deletions graphql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func main() {
db.CreateUsersTable()
db.CreateBlogPostsTable()
db.CreateCommentsTable()
db.CreateCategoriesTable()
db.CreateCategoryAssignmentsTable()

srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))

Expand Down
2 changes: 2 additions & 0 deletions user-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func main() {
db.CreateUsersTable()
db.CreateBlogPostsTable()
db.CreateCommentsTable()
db.CreateCategoriesTable()
db.CreateCategoryAssignmentsTable()
// Create a new Gin router
r := gin.Default()

Expand Down

0 comments on commit bca72b2

Please sign in to comment.