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.
Merge pull request #55 from Coderockr/feature/38
Issue #38 - Get Organization Endpoint
- Loading branch information
Showing
20 changed files
with
449 additions
and
40 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 |
---|---|---|
|
@@ -14,5 +14,8 @@ bin/* | |
|
||
# ignored until version 1.0 | ||
vendor | ||
vendor.orig | ||
|
||
coverage.txt | ||
server-cmd | ||
.data/ |
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,4 @@ | ||
-- +migrate Up | ||
ALTER TABLE needs RENAME categoty_id TO category_id; | ||
-- +migrate Down | ||
ALTER TABLE needs RENAME category_id TO categoty_id; |
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,6 @@ | ||
-- +migrate Up | ||
insert into needs_images (need_id, name, url) values(1, 'campanha alimentos', 'http://pibjc.org/novosite/wp-content/uploads/2016/11/campanha-alimentos.jpg'); | ||
insert into needs_images (need_id, name, url) values(1, 'outra campanha alimentos', 'http://pibjc.org/novosite/wp-content/uploads/2016/11/campanha-alimentos.jpg'); | ||
insert into organizations_images (organization_id, name, url) values(1, 'banner', 'http://info.geekie.com.br/wp-content/uploads/2015/04/Unesco.jpg'); | ||
insert into organizations_images (organization_id, name, url) values(1, 'SDG', 'http://en.unesco.org/sites/default/files/sdgs_poster_new1.png'); | ||
-- +migrate Down |
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,6 @@ | ||
|
||
-- +migrate Up | ||
select setval('organizations_id_seq', 2); | ||
select setval('needs_id_seq', 3); | ||
select setval('categories_id_seq', 9); | ||
-- +migrate Down |
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,5 @@ | ||
|
||
-- +migrate Up | ||
insert into needs (category_id, organization_id, title, description, required_qtd, reached_qtd, due_date, status, unity) | ||
values (5, 1, 'Voluntário', 'Ajudar a levar comida as áreas de risco', 100, 0, '2018-01-01', 'ACTIVE', 'semanas'); | ||
-- +migrate Down |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,5 @@ | ||
package auth | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrEmailDuplication = errors.New("The email is already in the store") | ||
ErrUserNotFound = errors.New("User not found") | ||
ErrWrongPassword = errors.New("email or password is incorrent") | ||
) | ||
|
||
type UserRepository interface { | ||
Login(email, pass string) (int64, error) | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ DATABASE_HOST=127.0.0.1 | |
DATABASE_PORT=5432 | ||
STORAGE=local | ||
STORAGE_PATH=/tmp | ||
API_PORT=8080 | ||
API_PORT=8000 |
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,38 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package handlers | ||
|
||
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.