Skip to content

Commit

Permalink
Refactor blog db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-dharti-r committed Apr 22, 2024
1 parent 08ac4bb commit f54c185
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion db/blog_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewBlogSql() *sqlx.DB {

var db *sqlx.DB

db, err := sqlx.Open("postgres", "postgres://"+username+":"+password+"@"+host+":"+port+"/"+name+"?sslmode=disable")
db, err := sqlx.Open("postgres", "postgres://"+username+":"+password+"@"+host+":"+port+"/"+name)
if err != nil {
fmt.Println("Error while connecting to the blog database: ", err)
}
Expand Down
19 changes: 11 additions & 8 deletions post/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,23 @@ func (repository *Repository) GetPosts(isFeatured, isResource, isPublished bool,
f.size, f.url, f.preview_url, f.provider, f.provider_metadata, f.folder_path, f.created_at, f.updated_at
FROM files f
JOIN files_related_morphs frm ON frm.related_id = $1 AND frm.related_type = 'api::post.post' AND frm.file_id = f.id`, post.Id)
if err != nil {
if err != nil && err != sql.ErrNoRows {
log.Error("Error while fetching post images: ", err)
return err, nil
}

var postFormats Formats
if post.Image.Format != "" {

if err := json.Unmarshal([]byte(post.Image.Format), &postFormats); err != nil {
log.Error("Error while unmarshal post image formats: ", err)
return err, nil
}
var postFormats Formats

post.Image.Format = ""
post.Image.Formats = postFormats
if err := json.Unmarshal([]byte(post.Image.Format), &postFormats); err != nil {
log.Error("Error while unmarshal post image formats: ", err)
return err, nil
}

post.Image.Format = ""
post.Image.Formats = postFormats
}

err = repository.Db.Get(&post.Author, `SELECT a.id, a.username, a.name, a.email, a.created_at, a.updated_at, a.bio
FROM authors a
Expand Down
4 changes: 2 additions & 2 deletions post/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Post struct {
IsFeatured bool `json:"is_featured"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
PublishedAt string `json:"published_at"`
PublishedAt null.String `json:"published_at"`
Summary string `json:"summary,omitempty"`
BlogContent string `json:"blog_content,omitempty"`
MetaDescription string `json:"meta_description,omitempty"`
Expand Down Expand Up @@ -117,7 +117,7 @@ type RecommendedPost struct {
IsFeatured bool `json:"is_featured"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
PublishedAt string `json:"published_at"`
PublishedAt null.String `json:"published_at"`
Summary string `json:"summary"`
BlogContent string `json:"blog_content"`
MetaDescription string `json:"meta_description"`
Expand Down

0 comments on commit f54c185

Please sign in to comment.