Skip to content

Commit

Permalink
add condition search on property api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshi Kurabayashi committed Jan 22, 2020
1 parent 11b8496 commit 3af381e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions infrastructure/api/handler/propertyHandler.go
Expand Up @@ -13,6 +13,7 @@ import (
type PropertyHandler interface {
GetAllProperties(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
GetProperty(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
GetPropertiesByPref(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
}

type propertyHandler struct {
Expand All @@ -33,3 +34,9 @@ func (ph *propertyHandler) GetProperty(w http.ResponseWriter, r *http.Request, p
prop, _ := ph.PropertyController.GetProperty(id)
json.NewEncoder(w).Encode(prop)
}

func (ph *propertyHandler) GetPropertiesByPref(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
pref_cd, _ := strconv.Atoi(ps.ByName("pref_cd"))
props, _ := ph.PropertyController.GetPropertiesByPref(pref_cd)
json.NewEncoder(w).Encode(props)
}
1 change: 1 addition & 0 deletions infrastructure/api/router/router.go
Expand Up @@ -15,4 +15,5 @@ func NewRouter(router *httprouter.Router, handler handler.AppHandler) {
// PROPERTY API
router.GET("/api/property/get", handler.PropertyHandler.GetAllProperties)
router.GET("/api/property/get/:id", handler.PropertyHandler.GetProperty)
router.GET("/api/property/pref/get/:pref_cd", handler.PropertyHandler.GetPropertiesByPref)
}
10 changes: 10 additions & 0 deletions infrastructure/datastore/propertyRepository.go
Expand Up @@ -9,6 +9,7 @@ import (
type PropertyRepository interface {
FindAll() (model.Properties, error)
FindById(id int) (model.Property, error)
FindByPref(pref_cd int) (model.Properties, error)
}

type propertyRepository struct {
Expand All @@ -33,3 +34,12 @@ func (pr *propertyRepository) FindById(id int) (model.Property, error) {
err := pr.db.Where("id = ?", id).First(&prop).Error
return prop, err
}

func (pr *propertyRepository) FindByPref(pref_cd int) (model.Properties, error) {
props := model.Properties{}
err := pr.db.Where("pref_cd = ?", pref_cd).Where("flg_open = ?", true).Order("pref_cd").Order("price").Find(&props).Error
if err != nil {
return nil, err
}
return props, nil
}
9 changes: 9 additions & 0 deletions interface/controllers/propertyController.go
Expand Up @@ -8,6 +8,7 @@ import (
type PropertyController interface {
GetAllProperties() (model.Properties, error)
GetProperty(id int) (model.Property, error)
GetPropertiesByPref(pref_cd int) (model.Properties, error)
}

type propertyController struct {
Expand All @@ -30,3 +31,11 @@ func (pc *propertyController) GetProperty(id int) (model.Property, error) {
prop, err := pc.PropertyService.Get(id)
return prop, err
}

func (pc *propertyController) GetPropertiesByPref(pref_cd int) (model.Properties, error) {
props, err := pc.PropertyService.GetByPref(pref_cd)
if err != nil {
return nil, err
}
return props, nil
}
1 change: 1 addition & 0 deletions usecase/repository/propertyRepository.go
Expand Up @@ -5,4 +5,5 @@ import "api/domain/model"
type PropertyRepository interface {
FindAll() (model.Properties, error)
FindById(id int) (model.Property, error)
FindByPref(pref_cd int) (model.Properties, error)
}
9 changes: 9 additions & 0 deletions usecase/service/propertyService.go
Expand Up @@ -9,6 +9,7 @@ import (
type PropertyService interface {
GetAll() (model.Properties, error)
Get(id int) (model.Property, error)
GetByPref(pref_cd int) (model.Properties, error)
}

type propertyService struct {
Expand All @@ -32,3 +33,11 @@ func (ps *propertyService) Get(id int) (model.Property, error) {
prop, err := ps.PropertyRepository.FindById(id)
return prop, err
}

func (ps *propertyService) GetByPref(pref_cd int) (model.Properties, error) {
props, err := ps.PropertyRepository.FindByPref(pref_cd)
if err != nil {
return nil, err
}
return ps.PropertyPresenter.ResponseProperties(props), nil
}

0 comments on commit 3af381e

Please sign in to comment.