This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #38 - Get Organization Endpoint
- Loading branch information
1 parent
3031381
commit c982f7c
Showing
7 changed files
with
387 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package repo | ||
|
||
import ( | ||
"github.com/golang/sync/syncmap" | ||
|
||
"github.com/Coderockr/vitrine-social/server/model" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// CategoryRepository to access database | ||
type CategoryRepository struct { | ||
db *sqlx.DB | ||
} | ||
|
||
var categoryCache = syncmap.Map{} | ||
|
||
// NewCategoryRepository create a new repository | ||
func NewCategoryRepository(db *sqlx.DB) *CategoryRepository { | ||
|
||
return &CategoryRepository{ | ||
db: db, | ||
} | ||
} | ||
|
||
// Get a category from database using its id | ||
func (r *CategoryRepository) Get(id int64) (model.Category, error) { | ||
if c, ok := categoryCache.Load(id); ok { | ||
return c.(model.Category), nil | ||
} | ||
|
||
c := model.Category{} | ||
err := r.db.Get(&c, "SELECT * FROM categories WHERE id = $1", id) | ||
if err != nil { | ||
return model.Category{}, err | ||
} | ||
|
||
categoryCache.Store(id, c) | ||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package repo | ||
|
||
import ( | ||
"github.com/Coderockr/vitrine-social/server/model" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// NeedRepository is a implementation for Postgres | ||
type NeedRepository struct { | ||
db *sqlx.DB | ||
catRepo *CategoryRepository | ||
} | ||
|
||
// NewNeedRepository creates a new repository | ||
func NewNeedRepository(db *sqlx.DB) *NeedRepository { | ||
return &NeedRepository{ | ||
db: db, | ||
catRepo: NewCategoryRepository(db), | ||
} | ||
} | ||
|
||
// Get one Need from database | ||
func (r *NeedRepository) Get(id int64) (*model.Need, error) { | ||
var n *model.Need | ||
err := r.db.Get(n, "SELECT * FROM needs WHERE id = $1", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
n.Category, err = r.catRepo.Get(n.CategoryID) | ||
return n, nil | ||
} | ||
|
||
// GetNeedImages without the need data | ||
func (r *NeedRepository) getNeedImages(n *model.Need) ([]model.NeedImage, error) { | ||
images := []model.NeedImage{} | ||
err := r.db.Select(&images, "SELECT * FROM needs_images WHERE need_id = $1", n.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return images, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package repo | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Coderockr/vitrine-social/server/model" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// OrganizationRepository is a implementation for Postgres | ||
type OrganizationRepository struct { | ||
db *sqlx.DB | ||
catRepo *CategoryRepository | ||
needRepo *NeedRepository | ||
} | ||
|
||
// NewOrganizationRepository creates a new repository | ||
func NewOrganizationRepository(db *sqlx.DB) *OrganizationRepository { | ||
return &OrganizationRepository{ | ||
db: db, | ||
catRepo: NewCategoryRepository(db), | ||
needRepo: NewNeedRepository(db), | ||
} | ||
} | ||
|
||
// Get one Organization from database | ||
func (r *OrganizationRepository) Get(id int64) (*model.Organization, error) { | ||
o := &model.Organization{} | ||
err := r.db.Get(o, "SELECT * FROM organizations WHERE id = $1", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = r.db.Select(&o.Images, "SELECT * FROM organizations_images WHERE organization_id = $1", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = r.db.Select(&o.Needs, "SELECT * FROM needs WHERE organization_id = $1", id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i := range o.Needs { | ||
o.Needs[i].Category, err = r.catRepo.Get(o.Needs[i].CategoryID) | ||
if err != nil { | ||
fmt.Println("test?") | ||
return nil, err | ||
} | ||
|
||
o.Needs[i].Images, err = r.needRepo.getNeedImages(&o.Needs[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return o, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package routes | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// ErrorMessage is a JSON formatter | ||
type ErrorMessage struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
// HandleHttpError formats and returns errors | ||
func HandleHttpError(w http.ResponseWriter, errno int, err error) { | ||
w.WriteHeader(errno) | ||
json.NewEncoder(w).Encode(&ErrorMessage{ | ||
Code: errno, | ||
Message: err.Error(), | ||
}) | ||
} |
Oops, something went wrong.