| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package friend
- import (
- "fmt"
- "net/http"
- "prime/user"
- . "prime/basic"
- "github.com/gin-gonic/gin"
- )
- func init() {
- r := user.Authorized
- r.GET("applications", getApplicationsHandler)
- r.POST("application", postApplicationHandler)
- r.PATCH("application", patchApplicationHandler)
- r.GET("friends", getFriendsHandler)
- }
- func getFriendsHandler(c *gin.Context) {
- uid := c.MustGet("uid").(uint)
- friends, err := GetFriends(uid)
- fmt.Println(friends)
- if err == nil {
- c.JSON(http.StatusOK, gin.H{"friends": friends})
- } else {
- c.JSON(ErrCode(err), gin.H{"error": err.Error()})
- }
- }
- func getApplicationsHandler(c *gin.Context) {
- uid := c.MustGet("uid").(uint)
- apps, err := GetApplications(uid)
- if err == nil {
- c.JSON(http.StatusOK, gin.H{"apps": apps})
- } else {
- c.JSON(ErrCode(err), gin.H{"error": err.Error()})
- }
- }
- func postApplicationHandler(c *gin.Context) {
- uid := c.MustGet("uid").(uint)
- var form struct {
- Receiver uint `json:"receiver" binding:"required"`
- Message []byte `json:"message" binding:"required"`
- }
- if err := c.Bind(&form); err == nil {
- appID, err := ApplyFriend(uid, form.Receiver, form.Message)
- if err == nil {
- c.JSON(http.StatusOK, gin.H{"appid": appID})
- } else {
- c.JSON(ErrCode(err), gin.H{"error": err.Error()})
- }
- } else {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- }
- }
- func patchApplicationHandler(c *gin.Context) {
- uid := c.MustGet("uid").(uint)
- fmt.Printf("uid=%d\n", uid)
- var form struct {
- AppID uint `json:"appid" binding:"required"`
- Reply int `json:"reply" binding:"required"`
- }
- if err := c.Bind(&form); err == nil {
- fmt.Printf("%+v\n", form)
- err := ReplyApplication(uid, form.AppID, form.Reply > 0)
- if err == nil {
- c.JSON(http.StatusOK, gin.H{"status": "ok"})
- } else {
- c.JSON(ErrCode(err), gin.H{"error": err.Error()})
- }
- } else {
- // TODO: 发送reply为0则无法读取???
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- panic(err)
- }
- }
|