Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Starting to remove more dbEntities into own folder and package

Matt Roberts1d5e19d315ee
diff --git a/app/services/sqlstore/dbEntities/comment.go b/app/services/sqlstore/dbEntities/comment.go
index 8f4e6bd02..3f3a4611a 100644
--- a/app/services/sqlstore/dbEntities/comment.go
+++ b/app/services/sqlstore/dbEntities/comment.go
@@ -9,7 +9,7 @@ import (
"github.com/getfider/fider/app/pkg/dbx"
)
-type comment struct {
+type Comment struct {
ID int `db:"id"`
Content string `db:"content"`
CreatedAt time.Time `db:"created_at"`
@@ -21,7 +21,7 @@ type comment struct {
IsApproved bool `db:"is_approved"`
}
-func (c *comment) toModel(ctx context.Context) *entity.Comment {
+func (c *Comment) ToModel(ctx context.Context) *entity.Comment {
comment := &entity.Comment{
ID: c.ID,
Content: c.Content,
diff --git a/app/services/sqlstore/dbEntities/post.go b/app/services/sqlstore/dbEntities/post.go
index e8f4e7901..07304cccf 100644
--- a/app/services/sqlstore/dbEntities/post.go
+++ b/app/services/sqlstore/dbEntities/post.go
@@ -10,7 +10,7 @@ import (
"github.com/lib/pq"
)
-type post struct {
+type Post struct {
ID int `db:"id"`
Number int `db:"number"`
Title string `db:"title"`
@@ -35,7 +35,7 @@ type post struct {
IsApproved bool `db:"is_approved"`
}
-func (i *post) toModel(ctx context.Context) *entity.Post {
+func (i *Post) ToModel(ctx context.Context) *entity.Post {
post := &entity.Post{
ID: i.ID,
Number: i.Number,
diff --git a/app/services/sqlstore/dbEntities/tenant.go b/app/services/sqlstore/dbEntities/tenant.go
index 3d89d6fe6..49f1b078c 100644
--- a/app/services/sqlstore/dbEntities/tenant.go
+++ b/app/services/sqlstore/dbEntities/tenant.go
@@ -24,7 +24,7 @@ type Tenant struct {
IsModerationEnabled bool `db:"is_moderation_enabled"`
}
-func (t *Tenant) toModel() *entity.Tenant {
+func (t *Tenant) ToModel() *entity.Tenant {
if t == nil {
return nil
}
diff --git a/app/services/sqlstore/dbEntities/user.go b/app/services/sqlstore/dbEntities/user.go
index cf0fc6abe..c08fb2614 100644
--- a/app/services/sqlstore/dbEntities/user.go
+++ b/app/services/sqlstore/dbEntities/user.go
@@ -42,7 +42,7 @@ func (u *User) ToModel(ctx context.Context) *entity.User {
tenant, ok := ctx.Value(app.TenantCtxKey).(*entity.Tenant)
if !ok || tenant == nil {
- tenant = u.Tenant.toModel()
+ tenant = u.Tenant.ToModel()
}
avatarType := enum.AvatarType(u.AvatarType.Int64)
diff --git a/app/services/sqlstore/dbEntities/verification.go b/app/services/sqlstore/dbEntities/verification.go
index 4fdacbe82..be01b5d66 100644
--- a/app/services/sqlstore/dbEntities/verification.go
+++ b/app/services/sqlstore/dbEntities/verification.go
@@ -8,7 +8,7 @@ import (
"github.com/getfider/fider/app/pkg/dbx"
)
-type emailVerification struct {
+type EmailVerification struct {
ID int `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
@@ -20,7 +20,7 @@ type emailVerification struct {
VerifiedAt dbx.NullTime `db:"verified_at"`
}
-func (t *emailVerification) toModel() *entity.EmailVerification {
+func (t *EmailVerification) ToModel() *entity.EmailVerification {
model := &entity.EmailVerification{
Name: t.Name,
Email: t.Email,
diff --git a/app/services/sqlstore/postgres/comment.go b/app/services/sqlstore/postgres/comment.go
index 95492c644..2b30a987e 100644
--- a/app/services/sqlstore/postgres/comment.go
+++ b/app/services/sqlstore/postgres/comment.go
@@ -11,39 +11,9 @@ import (
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/errors"
+ "github.com/getfider/fider/app/services/sqlstore/dbEntities"
)
-type dbComment struct {
- ID int `db:"id"`
- Content string `db:"content"`
- CreatedAt time.Time `db:"created_at"`
- User *dbUser `db:"user"`
- Attachments []string `db:"attachment_bkeys"`
- EditedAt dbx.NullTime `db:"edited_at"`
- EditedBy *dbUser `db:"edited_by"`
- ReactionCounts dbx.NullString `db:"reaction_counts"`
- IsApproved bool `db:"is_approved"`
-}
-
-func (c *dbComment) toModel(ctx context.Context) *entity.Comment {
- comment := &entity.Comment{
- ID: c.ID,
- Content: c.Content,
- CreatedAt: c.CreatedAt,
- User: c.User.toModel(ctx),
- Attachments: c.Attachments,
- IsApproved: c.IsApproved,
- }
- if c.EditedAt.Valid {
- comment.EditedBy = c.EditedBy.toModel(ctx)
- comment.EditedAt = &c.EditedAt.Time
- }
-
- if c.ReactionCounts.Valid {
- _ = json.Unmarshal([]byte(c.ReactionCounts.String), &comment.ReactionCounts)
- }
- return comment
-}
func addNewComment(ctx context.Context, c *cmd.AddNewComment) error {
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
@@ -127,7 +97,7 @@ func getCommentByID(ctx context.Context, q *query.GetCommentByID) error {
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
q.Result = nil
- comment := dbComment{}
+ comment := dbEntities.Comment{}
err := trx.Get(&comment,
`SELECT c.id,
c.content,
@@ -163,7 +133,7 @@ func getCommentByID(ctx context.Context, q *query.GetCommentByID) error {
return err
}
- q.Result = comment.toModel(ctx)
+ q.Result = comment.ToModel(ctx)
return nil
})
}
@@ -172,7 +142,7 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error {
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
q.Result = make([]*entity.Comment, 0)
- comments := []*dbComment{}
+ comments := []*dbEntities.Comment{}
userId := 0
if user != nil {
userId = user.ID
@@ -273,7 +243,7 @@ func getCommentsByPost(ctx context.Context, q *query.GetCommentsByPost) error {
q.Result = make([]*entity.Comment, len(comments))
for i, comment := range comments {
- q.Result[i] = comment.toModel(ctx)
+ q.Result[i] = comment.ToModel(ctx)
}
return nil
})
diff --git a/app/services/sqlstore/postgres/tenant.go b/app/services/sqlstore/postgres/tenant.go
index c5971a457..1f71c7f5d 100644
--- a/app/services/sqlstore/postgres/tenant.go
+++ b/app/services/sqlstore/postgres/tenant.go
@@ -5,6 +5,7 @@ import (
"time"
"github.com/getfider/fider/app/pkg/bus"
+ "github.com/getfider/fider/app/services/sqlstore/dbEntities"
"github.com/getfider/fider/app/models/cmd"
"github.com/getfider/fider/app/models/entity"
@@ -16,85 +17,7 @@ import (
"github.com/getfider/fider/app/pkg/errors"
)
-type dbTenant struct {
- ID int `db:"id"`
- Name string `db:"name"`
- Subdomain string `db:"subdomain"`
- CNAME string `db:"cname"`
- Invitation string `db:"invitation"`
- WelcomeMessage string `db:"welcome_message"`
- Status int `db:"status"`
- Locale string `db:"locale"`
- IsPrivate bool `db:"is_private"`
- LogoBlobKey string `db:"logo_bkey"`
- CustomCSS string `db:"custom_css"`
- AllowedSchemes string `db:"allowed_schemes"`
- IsEmailAuthAllowed bool `db:"is_email_auth_allowed"`
- IsFeedEnabled bool `db:"is_feed_enabled"`
- PreventIndexing bool `db:"prevent_indexing"`
- IsModerationEnabled bool `db:"is_moderation_enabled"`
-}
-
-func (t *dbTenant) toModel() *entity.Tenant {
- if t == nil {
- return nil
- }
-
- tenant := &entity.Tenant{
- ID: t.ID,
- Name: t.Name,
- Subdomain: t.Subdomain,
- CNAME: t.CNAME,
- Invitation: t.Invitation,
- WelcomeMessage: t.WelcomeMessage,
- Status: enum.TenantStatus(t.Status),
- Locale: t.Locale,
- IsPrivate: t.IsPrivate,
- LogoBlobKey: t.LogoBlobKey,
- CustomCSS: t.CustomCSS,
- AllowedSchemes: t.AllowedSchemes,
- IsEmailAuthAllowed: t.IsEmailAuthAllowed,
- IsFeedEnabled: t.IsFeedEnabled,
- PreventIndexing: t.PreventIndexing,
- IsModerationEnabled: t.IsModerationEnabled,
- }
-
- return tenant
-}
-type dbEmailVerification struct {
- ID int `db:"id"`
- Name string `db:"name"`
- Email string `db:"email"`
- Key string `db:"key"`
- Kind enum.EmailVerificationKind `db:"kind"`
- UserID dbx.NullInt `db:"user_id"`
- CreatedAt time.Time `db:"created_at"`
- ExpiresAt time.Time `db:"expires_at"`
- VerifiedAt dbx.NullTime `db:"verified_at"`
-}
-
-func (t *dbEmailVerification) toModel() *entity.EmailVerification {
- model := &entity.EmailVerification{
- Name: t.Name,
- Email: t.Email,
- Key: t.Key,
- Kind: t.Kind,
- CreatedAt: t.CreatedAt,
- ExpiresAt: t.ExpiresAt,
- VerifiedAt: nil,
- }
-
- if t.VerifiedAt.Valid {
- model.VerifiedAt = &t.VerifiedAt.Time
- }
-
- if t.UserID.Valid {
- model.UserID = int(t.UserID.Int64)
- }
-
- return model
-}
func isCNAMEAvailable(ctx context.Context, q *query.IsCNAMEAvailable) error {
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
@@ -206,7 +129,7 @@ func activateTenant(ctx context.Context, c *cmd.ActivateTenant) error {
func getVerificationByKey(ctx context.Context, q *query.GetVerificationByKey) error {
return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
- verification := dbEmailVerification{}
+ verification := dbEntities.EmailVerification{}
query := "SELECT id, email, name, key, created_at, verified_at, expires_at, kind, user_id FROM email_verifications WHERE key = $1 AND kind = $2 LIMIT 1"
err := trx.Get(&verification, query, q.Key, q.Kind)
@@ -214,7 +137,7 @@ func getVerificationByKey(ctx context.Context, q *query.GetVerificationByKey) er
return errors.Wrap(err, "failed to get email verification by its key")
}
- q.Result = verification.toModel()
+ q.Result = verification.ToModel()
return nil
})
}
@@ -278,7 +201,7 @@ func createTenant(ctx context.Context, c *cmd.CreateTenant) error {
func getFirstTenant(ctx context.Context, q *query.GetFirstTenant) error {
return using(ctx, func(trx *dbx.Trx, _ *entity.Tenant, _ *entity.User) error {
- tenant := dbTenant{}
+ tenant := dbEntities.Tenant{}
err := trx.Get(&tenant, `
SELECT id, name, subdomain, cname, invitation, locale, welcome_message, status, is_private, logo_bkey, custom_css, allowed_schemes, is_email_auth_allowed, is_feed_enabled, prevent_indexing
@@ -289,14 +212,14 @@ func getFirstTenant(ctx context.Context, q *query.GetFirstTenant) error {
return errors.Wrap(err, "failed to get first tenant")
}
- q.Result = tenant.toModel()
+ q.Result = tenant.ToModel()
return nil
})
}
func getTenantByDomain(ctx context.Context, q *query.GetTenantByDomain) error {
return using(ctx, func(trx *dbx.Trx, _ *entity.Tenant, _ *entity.User) error {
- tenant := dbTenant{}
+ tenant := dbEntities.Tenant{}
err := trx.Get(&tenant, `
SELECT id, name, subdomain, cname, invitation, locale, welcome_message, status, is_private, logo_bkey, custom_css, is_email_auth_allowed, is_feed_enabled, is_moderation_enabled, prevent_indexing
@@ -308,7 +231,7 @@ func getTenantByDomain(ctx context.Context, q *query.GetTenantByDomain) error {
return errors.Wrap(err, "failed to get tenant with domain '%s'", q.Domain)
}
- q.Result = tenant.toModel()
+ q.Result = tenant.ToModel()
return nil
})
}
diff --git a/app/services/sqlstore/postgres/user.go b/app/services/sqlstore/postgres/user.go
index 944288dba..a8517f651 100644
--- a/app/services/sqlstore/postgres/user.go
+++ b/app/services/sqlstore/postgres/user.go
@@ -15,19 +15,20 @@ import (
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/errors"
+ "github.com/getfider/fider/app/services/sqlstore/dbEntities"
"github.com/lib/pq"
)
type dbUser struct {
- ID sql.NullInt64 `db:"id"`
- Name sql.NullString `db:"name"`
- Email sql.NullString `db:"email"`
- Tenant *dbTenant `db:"tenant"`
- Role sql.NullInt64 `db:"role"`
- Status sql.NullInt64 `db:"status"`
- AvatarType sql.NullInt64 `db:"avatar_type"`
- AvatarBlobKey sql.NullString `db:"avatar_bkey"`
- IsVerified sql.NullBool `db:"is_verified"`
+ ID sql.NullInt64 `db:"id"`
+ Name sql.NullString `db:"name"`
+ Email sql.NullString `db:"email"`
+ Tenant *dbEntities.Tenant `db:"tenant"`
+ Role sql.NullInt64 `db:"role"`
+ Status sql.NullInt64 `db:"status"`
+ AvatarType sql.NullInt64 `db:"avatar_type"`
+ AvatarBlobKey sql.NullString `db:"avatar_bkey"`
+ IsVerified sql.NullBool `db:"is_verified"`
Providers []*dbUserProvider
}
@@ -51,7 +52,7 @@ func (u *dbUser) toModel(ctx context.Context) *entity.User {
ID: int(u.ID.Int64),
Name: u.Name.String,
Email: u.Email.String,
- Tenant: u.Tenant.toModel(),
+ Tenant: u.Tenant.ToModel(),
Role: enum.Role(u.Role.Int64),
Providers: make([]*entity.UserProvider, len(u.Providers)),
Status: enum.UserStatus(u.Status.Int64),