welcomes.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package models
  2. import "time"
  3. type Welcome struct {
  4. ID uint `gorm:"primary_key" json:"id"`
  5. UserId string `json:"user_id"`
  6. Content string `json:"content"`
  7. IsDefault uint `json:"is_default"`
  8. Ctime time.Time `json:"ctime"`
  9. }
  10. func CreateWelcome(userId string, content string) uint {
  11. if userId == "" || content == "" {
  12. return 0
  13. }
  14. w := &Welcome{
  15. UserId: userId,
  16. Content: content,
  17. Ctime: time.Now(),
  18. }
  19. DB.Create(w)
  20. return w.ID
  21. }
  22. func UpdateWelcome(userId string, id string, content string) uint {
  23. if userId == "" || content == "" {
  24. return 0
  25. }
  26. w := &Welcome{
  27. Content: content,
  28. }
  29. DB.Model(w).Where("user_id = ? and id = ?", userId, id).Update(w)
  30. return w.ID
  31. }
  32. func FindWelcomeByUserId(userId interface{}) Welcome {
  33. var w Welcome
  34. DB.Where("user_id = ? and is_default=?", userId, 1).First(&w)
  35. return w
  36. }
  37. func FindWelcomesByUserId(userId interface{}) []Welcome {
  38. var w []Welcome
  39. DB.Where("user_id = ?", userId).Find(&w)
  40. return w
  41. }
  42. func DeleteWelcome(userId interface{}, id string) {
  43. DB.Where("user_id = ? and id = ?", userId, id).Delete(Welcome{})
  44. }