welcomes.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package models
  2. import "time"
  3. type Welcome struct {
  4. ID uint `gorm:"primaryKey" json:"id"`
  5. UserId string `gorm:"size:100 not null default:'' index:'user_id'" json:"user_id"`
  6. Content string `gorm:"size:512 not null default:''" json:"content"`
  7. IsDefault uint `gorm:"size:3 not null default:0" json:"is_default"`
  8. CreatedAt 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. }
  18. DB.Create(w)
  19. return w.ID
  20. }
  21. func UpdateWelcome(userId string, id string, content string) uint {
  22. if userId == "" || content == "" {
  23. return 0
  24. }
  25. w := &Welcome{
  26. Content: content,
  27. }
  28. DB.Model(w).Where("user_id = ? and id = ?", userId, id).Updates(w)
  29. return w.ID
  30. }
  31. func FindWelcomeByUserId(userId interface{}) Welcome {
  32. var w Welcome
  33. DB.Where("user_id = ? and is_default=?", userId, 1).First(&w)
  34. return w
  35. }
  36. func FindWelcomesByUserId(userId interface{}) []Welcome {
  37. var w []Welcome
  38. DB.Where("user_id = ?", userId).Find(&w)
  39. return w
  40. }
  41. func DeleteWelcome(userId interface{}, id string) {
  42. DB.Where("user_id = ? and id = ?", userId, id).Delete(Welcome{})
  43. }