Skip to content

Commit 04dd646

Browse files
Add solution for gorm challenge-2-associations (#853)
Co-authored-by: go-interview-practice-bot[bot] <230190823+go-interview-practice-bot[bot]@users.noreply.github.com>
1 parent a6d924a commit 04dd646

File tree

1 file changed

+117
-0
lines changed
  • packages/gorm/challenge-2-associations/submissions/aswinsreeraj

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
"gorm.io/gorm"
7+
"gorm.io/driver/sqlite"
8+
)
9+
10+
// User represents a user in the blog system
11+
type User struct {
12+
ID uint `gorm:"primaryKey"`
13+
Name string `gorm:"not null"`
14+
Email string `gorm:"unique;not null"`
15+
Posts []Post `gorm:"foreignKey:UserID"`
16+
CreatedAt time.Time
17+
UpdatedAt time.Time
18+
}
19+
20+
// Post represents a blog post
21+
type Post struct {
22+
ID uint `gorm:"primaryKey"`
23+
Title string `gorm:"not null"`
24+
Content string `gorm:"type:text"`
25+
UserID uint `gorm:"not null"`
26+
User User `gorm:"foreignKey:UserID"`
27+
Tags []Tag `gorm:"many2many:post_tags;"`
28+
CreatedAt time.Time
29+
UpdatedAt time.Time
30+
}
31+
32+
// Tag represents a tag for categorizing posts
33+
type Tag struct {
34+
ID uint `gorm:"primaryKey"`
35+
Name string `gorm:"unique;not null"`
36+
Posts []Post `gorm:"many2many:post_tags;"`
37+
}
38+
39+
// ConnectDB establishes a connection to the SQLite database and auto-migrates the models
40+
func ConnectDB() (*gorm.DB, error) {
41+
// TODO: Implement database connection with auto-migration
42+
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
43+
if err != nil {
44+
return nil, err
45+
}
46+
err = db.AutoMigrate(&User{}, &Post{}, &Tag{})
47+
return db, err
48+
}
49+
50+
// CreateUserWithPosts creates a new user with associated posts
51+
func CreateUserWithPosts(db *gorm.DB, user *User) error {
52+
// TODO: Implement user creation with posts
53+
result := db.Create(user)
54+
return result.Error
55+
}
56+
57+
// GetUserWithPosts retrieves a user with all their posts preloaded
58+
func GetUserWithPosts(db *gorm.DB, userID uint) (*User, error) {
59+
// TODO: Implement user retrieval with posts
60+
var user User
61+
result := db.Preload("Posts").First(&user, userID)
62+
return &user, result.Error
63+
}
64+
65+
// CreatePostWithTags creates a new post with specified tags
66+
func CreatePostWithTags(db *gorm.DB, post *Post, tagNames []string) error {
67+
// TODO: Implement post creation with tags
68+
if err := db.Create(post).Error; err != nil {
69+
return err
70+
}
71+
for _, name := range tagNames {
72+
var tag Tag
73+
db.FirstOrCreate(&tag, Tag{Name: name})
74+
db.Model(post).Association("Tags").Append(&tag)
75+
}
76+
return nil
77+
}
78+
79+
// GetPostsByTag retrieves all posts that have a specific tag
80+
func GetPostsByTag(db *gorm.DB, tagName string) ([]Post, error) {
81+
// TODO: Implement posts retrieval by tag
82+
var posts []Post
83+
err := db.Joins("JOIN post_tags ON posts.id = post_tags.post_id").
84+
Joins("JOIN tags ON post_tags.tag_id = tags.id").
85+
Where("tags.name = ?", tagName).
86+
Find(&posts).Error
87+
return posts, err
88+
}
89+
90+
// AddTagsToPost adds tags to an existing post
91+
func AddTagsToPost(db *gorm.DB, postID uint, tagNames []string) error {
92+
// TODO: Implement adding tags to existing post
93+
var post Post
94+
result := db.First(&post, postID)
95+
if result.Error != nil {
96+
return result.Error
97+
}
98+
for _, tagName := range tagNames {
99+
var tag Tag
100+
db.FirstOrCreate(&tag, Tag{Name: tagName})
101+
if err := db.Model(&post).Association("Tags").Append(&tag); err != nil {
102+
return err
103+
}
104+
}
105+
return nil
106+
}
107+
108+
// GetPostWithUserAndTags retrieves a post with user and tags preloaded
109+
func GetPostWithUserAndTags(db *gorm.DB, postID uint) (*Post, error) {
110+
// TODO: Implement post retrieval with user and tags
111+
var post Post
112+
err := db.Preload("User").Preload("Tags").First(&post, postID).Error
113+
if err != nil {
114+
return nil, err
115+
}
116+
return &post, nil
117+
}

0 commit comments

Comments
 (0)