|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "github.com/gin-gonic/gin" |
| 7 | + "gorm.io/gorm" |
| 8 | + "math/rand" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type Blog struct { |
| 14 | + gorm.Model |
| 15 | + //User |
| 16 | + UserId uint |
| 17 | + //一对多reply |
| 18 | + Replies []Reply `gorm:"foreignKey:BlogID"` |
| 19 | + |
| 20 | + ID uint |
| 21 | + Time string |
| 22 | + Content string `json:"content"` |
| 23 | + Title string `json:"title"` |
| 24 | + Author string `json:"author"` |
| 25 | + Type string `json:"type"` |
| 26 | +} |
| 27 | + |
| 28 | +type Reply struct { |
| 29 | + gorm.Model |
| 30 | + //User |
| 31 | + UserId uint |
| 32 | + //blog |
| 33 | + BlogID uint `json:"BlogID"` |
| 34 | + Body string `json:"body"` |
| 35 | + Who string `json:"who"` |
| 36 | +} |
| 37 | + |
| 38 | +// 显示我的提问与回复 |
| 39 | +func mine(c *gin.Context) { |
| 40 | + cookieValue, err := loadCookie(c) |
| 41 | + userId, _ := base64.StdEncoding.DecodeString(cookieValue) |
| 42 | + if err != nil { |
| 43 | + c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + //查找user下的所有blog和blog下的所有reply |
| 48 | + var user User |
| 49 | + // 先通过 username 查询 User |
| 50 | + if err := db.Where("username = ?", userId).First(&user).Error; err != nil { |
| 51 | + c.JSON(http.StatusNotFound, gin.H{"error": "User not found"}) |
| 52 | + return |
| 53 | + } |
| 54 | + // 通过预加载获取该用户的所有 Blogs 和对应的 Replies |
| 55 | + if err := db.Preload("Blogs.Replies").First(&user, user.ID).Error; err != nil { |
| 56 | + c.JSON(http.StatusNotFound, gin.H{"error": "Blogs not found"}) |
| 57 | + return |
| 58 | + } |
| 59 | + c.JSON(http.StatusOK, gin.H{"user": user}) |
| 60 | + return |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +// 显示其他人的问题 |
| 65 | +func others(c *gin.Context) { |
| 66 | + var blogs []Blog |
| 67 | + // 预加载所有 Blogs 及其对应的 Replies |
| 68 | + if err := db.Preload("Replies").Find(&blogs).Error; err != nil { |
| 69 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve blogs"}) |
| 70 | + return |
| 71 | + } |
| 72 | + c.JSON(http.StatusOK, gin.H{"blogs": blogs}) |
| 73 | + return |
| 74 | +} |
| 75 | + |
| 76 | +// 创建我的新问题 |
| 77 | +func mineNews(c *gin.Context) { |
| 78 | + cookieValue, err := loadCookie(c) |
| 79 | + userId, _ := base64.StdEncoding.DecodeString(cookieValue) |
| 80 | + if err != nil { |
| 81 | + c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + var blogs Blog |
| 86 | + blogs.ID = uint(rand.Uint32()) |
| 87 | + blogs.Time = time.Now().Format("2006-01-02 15:04:05") |
| 88 | + if err := c.ShouldBind(&blogs); err != nil { |
| 89 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + var user User |
| 94 | + if err := db.Where("username = ?", string(userId)).First(&user).Error; err != nil { |
| 95 | + c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) |
| 96 | + return |
| 97 | + } |
| 98 | + //键赋值 |
| 99 | + blogs.UserId = user.ID |
| 100 | + |
| 101 | + if err := db.Create(&blogs).Error; err != nil { |
| 102 | + c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + c.JSON(http.StatusOK, gin.H{"blogs": blogs}) |
| 107 | + fmt.Println("成功发布问题") |
| 108 | +} |
| 109 | + |
| 110 | +func reply(c *gin.Context) { |
| 111 | + cookieValue, err := loadCookie(c) |
| 112 | + userId, _ := base64.StdEncoding.DecodeString(cookieValue) |
| 113 | + if err != nil { |
| 114 | + c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) |
| 115 | + } |
| 116 | + |
| 117 | + // 检查用户是否存在 |
| 118 | + var user User |
| 119 | + if err := db.Where("username = ?", userId).First(&user).Error; err != nil { |
| 120 | + c.JSON(http.StatusNotFound, gin.H{"error": "User not found"}) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + //创建回复 |
| 125 | + var reply Reply |
| 126 | + if err := c.ShouldBind(&reply); err != nil { |
| 127 | + |
| 128 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 129 | + } |
| 130 | + |
| 131 | + //键赋值 |
| 132 | + reply.UserId = user.ID |
| 133 | + //上传回复 |
| 134 | + if err := db.Create(&reply).Error; err != nil { |
| 135 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create reply"}) |
| 136 | + } |
| 137 | + c.JSON(http.StatusOK, gin.H{"reply": reply}) |
| 138 | +} |
| 139 | + |
| 140 | +func blogEdit(c *gin.Context) { |
| 141 | + |
| 142 | + var blog Blog |
| 143 | + blogID := c.Param("id") // 获取 URL 中的博客 ID |
| 144 | + |
| 145 | + // 查找博客 |
| 146 | + if err := db.First(&blog, blogID).Error; err != nil { |
| 147 | + c.JSON(http.StatusNotFound, gin.H{"error": "Blog not found"}) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + // 绑定更新数据 |
| 152 | + if err := c.ShouldBind(&blog); err != nil { |
| 153 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + // 更新博客 |
| 158 | + if err := db.Save(&blog).Error; err != nil { |
| 159 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update blog"}) |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + c.JSON(http.StatusOK, gin.H{"blog": blog}) |
| 164 | +} |
| 165 | + |
| 166 | +func blogDelete(c *gin.Context) { |
| 167 | + |
| 168 | + blogID := c.Param("id") // 获取 URL 中的博客 ID |
| 169 | + var blog Blog |
| 170 | + |
| 171 | + // 查找博客 |
| 172 | + if err := db.First(&blog, blogID).Error; err != nil { |
| 173 | + c.JSON(http.StatusNotFound, gin.H{"error": "Blog not found"}) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + // 删除博客 |
| 178 | + if err := db.Delete(&blog).Error; err != nil { |
| 179 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete blog"}) |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + c.JSON(http.StatusOK, gin.H{"message": "Blog deleted successfully"}) |
| 184 | +} |
0 commit comments