helpers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package logic
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "time"
  7. "udeServer/internal/apierr"
  8. "udeServer/internal/model"
  9. "udeServer/internal/types"
  10. )
  11. func nullString(v string) sql.NullString {
  12. return sql.NullString{String: v, Valid: v != ""}
  13. }
  14. func nullInt64(v int64) sql.NullInt64 {
  15. return sql.NullInt64{Int64: v, Valid: v != 0}
  16. }
  17. func stringValue(v sql.NullString) string {
  18. if !v.Valid {
  19. return ""
  20. }
  21. return v.String
  22. }
  23. func int64Value(v sql.NullInt64) int64 {
  24. if !v.Valid {
  25. return 0
  26. }
  27. return v.Int64
  28. }
  29. func timeValue(t time.Time) string {
  30. if t.IsZero() {
  31. return ""
  32. }
  33. return t.Format(time.RFC3339)
  34. }
  35. func checkId(id int64) error {
  36. if id <= 0 {
  37. return apierr.BadRequest("id must be greater than 0")
  38. }
  39. return nil
  40. }
  41. func findDirectory(ctx context.Context, m model.TDirectoryModel, id int64) (*model.TDirectory, error) {
  42. data, err := m.FindOne(ctx, id)
  43. if errors.Is(err, model.ErrNotFound) {
  44. return nil, apierr.NotFound("directory", id)
  45. }
  46. if err != nil {
  47. return nil, err
  48. }
  49. return data, nil
  50. }
  51. func findKey(ctx context.Context, m model.TKeyModel, id int64) (*model.TKey, error) {
  52. data, err := m.FindOne(ctx, id)
  53. if errors.Is(err, model.ErrNotFound) {
  54. return nil, apierr.NotFound("key", id)
  55. }
  56. if err != nil {
  57. return nil, err
  58. }
  59. return data, nil
  60. }
  61. func findService(ctx context.Context, m model.TServiceModel, id int64) (*model.TService, error) {
  62. data, err := m.FindOne(ctx, id)
  63. if errors.Is(err, model.ErrNotFound) {
  64. return nil, apierr.NotFound("service", id)
  65. }
  66. if err != nil {
  67. return nil, err
  68. }
  69. return data, nil
  70. }
  71. func toDirectoryResp(data *model.TDirectory) *types.DirectoryResp {
  72. return &types.DirectoryResp{
  73. Id: data.Id,
  74. ServiceId: int64Value(data.ServiceId),
  75. Directory: stringValue(data.Directory),
  76. }
  77. }
  78. func toKeyResp(data *model.TKey) *types.KeyResp {
  79. return &types.KeyResp{
  80. Id: data.Id,
  81. Iden: stringValue(data.Iden),
  82. Name: stringValue(data.Name),
  83. Content: stringValue(data.Content),
  84. Status: stringValue(data.Status),
  85. CreateAt: timeValue(data.CreateAt),
  86. }
  87. }
  88. func toServiceResp(data *model.TService) *types.ServiceResp {
  89. return &types.ServiceResp{
  90. Id: data.Id,
  91. Name: stringValue(data.Name),
  92. Ip: stringValue(data.Ip),
  93. Port: data.Port,
  94. Iden: stringValue(data.Iden),
  95. KeyIden: stringValue(data.KeyIden),
  96. KeyAlgo: stringValue(data.KeyAlgo),
  97. Status: stringValue(data.Status),
  98. MountStatus: stringValue(data.MountStatus),
  99. Brush: data.Brush,
  100. CreateAt: timeValue(data.CreateAt),
  101. }
  102. }