diff --git a/api/region.go b/api/region.go new file mode 100644 index 0000000..c84ec12 --- /dev/null +++ b/api/region.go @@ -0,0 +1,37 @@ +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (server *Server) getListRegions(ctx *gin.Context) { + regions, err := server.Store.GetListRegions(ctx) + if err != nil { + ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to get regions")) + return + } + + ctx.JSON(http.StatusOK, regions) +} + +func (server *Server) getListProvinces(ctx *gin.Context) { + provinces, err := server.Store.GetListProvinces(ctx) + if err != nil { + ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to get regions")) + return + } + + ctx.JSON(http.StatusOK, provinces) +} + +func (server *Server) getListRegencies(ctx *gin.Context) { + regencies, err := server.Store.GetListRegencies(ctx) + if err != nil { + ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while try to get regions")) + return + } + + ctx.JSON(http.StatusOK, regencies) +} diff --git a/api/server.go b/api/server.go index fc0701a..80fd346 100644 --- a/api/server.go +++ b/api/server.go @@ -48,6 +48,9 @@ func (server *Server) getRoutes() { router.POST("/user/signup", server.createUser) router.POST("/user/login", server.login) router.POST("/user/logout", server.logout) + router.GET("/regions", server.getListRegions) + router.GET("/region/provinces", server.getListProvinces) + router.GET("/region/regencies", server.getListRegencies) // LOCATION router.POST("/locations", server.createLocation) diff --git a/db/mock/store.go b/db/mock/store.go index 13d3147..e39f631 100644 --- a/db/mock/store.go +++ b/db/mock/store.go @@ -169,6 +169,21 @@ func (mr *MockStoreMockRecorder) GetListLocations(arg0 interface{}) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetListLocations", reflect.TypeOf((*MockStore)(nil).GetListLocations), arg0) } +// GetListProvinces mocks base method. +func (m *MockStore) GetListProvinces(arg0 context.Context) ([]db.GetListProvincesRow, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetListProvinces", arg0) + ret0, _ := ret[0].([]db.GetListProvincesRow) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetListProvinces indicates an expected call of GetListProvinces. +func (mr *MockStoreMockRecorder) GetListProvinces(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetListProvinces", reflect.TypeOf((*MockStore)(nil).GetListProvinces), arg0) +} + // GetListRecentLocationsWithRatings mocks base method. func (m *MockStore) GetListRecentLocationsWithRatings(arg0 context.Context, arg1 int32) ([]db.GetListRecentLocationsWithRatingsRow, error) { m.ctrl.T.Helper() @@ -184,6 +199,36 @@ func (mr *MockStoreMockRecorder) GetListRecentLocationsWithRatings(arg0, arg1 in return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetListRecentLocationsWithRatings", reflect.TypeOf((*MockStore)(nil).GetListRecentLocationsWithRatings), arg0, arg1) } +// GetListRegencies mocks base method. +func (m *MockStore) GetListRegencies(arg0 context.Context) ([]db.GetListRegenciesRow, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetListRegencies", arg0) + ret0, _ := ret[0].([]db.GetListRegenciesRow) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetListRegencies indicates an expected call of GetListRegencies. +func (mr *MockStoreMockRecorder) GetListRegencies(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetListRegencies", reflect.TypeOf((*MockStore)(nil).GetListRegencies), arg0) +} + +// GetListRegions mocks base method. +func (m *MockStore) GetListRegions(arg0 context.Context) ([]db.GetListRegionsRow, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetListRegions", arg0) + ret0, _ := ret[0].([]db.GetListRegionsRow) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetListRegions indicates an expected call of GetListRegions. +func (mr *MockStoreMockRecorder) GetListRegions(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetListRegions", reflect.TypeOf((*MockStore)(nil).GetListRegions), arg0) +} + // GetLocation mocks base method. func (m *MockStore) GetLocation(arg0 context.Context, arg1 int32) (db.GetLocationRow, error) { m.ctrl.T.Helper() diff --git a/db/queries/provinces.sql b/db/queries/provinces.sql new file mode 100644 index 0000000..bbea402 --- /dev/null +++ b/db/queries/provinces.sql @@ -0,0 +1,6 @@ +-- name: GetListProvinces :many +SELECT +id, +province_name, +region_id +FROM PROVINCES; \ No newline at end of file diff --git a/db/queries/regencies.sql b/db/queries/regencies.sql new file mode 100644 index 0000000..83050a7 --- /dev/null +++ b/db/queries/regencies.sql @@ -0,0 +1,6 @@ +-- name: GetListRegencies :many +SELECT +id, +regency_name, +province_id +FROM regencies; \ No newline at end of file diff --git a/db/queries/regions.sql b/db/queries/regions.sql new file mode 100644 index 0000000..597a899 --- /dev/null +++ b/db/queries/regions.sql @@ -0,0 +1,5 @@ +-- name: GetListRegions :many +SELECT +id, +region_name +FROM regions; \ No newline at end of file diff --git a/db/sqlc/locations.sql.go b/db/sqlc/locations.sql.go index c227124..c41e94c 100644 --- a/db/sqlc/locations.sql.go +++ b/db/sqlc/locations.sql.go @@ -42,7 +42,7 @@ func (q *Queries) CreateLocation(ctx context.Context, arg CreateLocationParams) } const getListLocations = `-- name: GetListLocations :many -SELECT id, address, name, google_maps_link, submitted_by, total_visited, thumbnail, regency_id, is_deleted, created_at, updated_at, approved_by, approved_at FROM locations +SELECT id, address, name, google_maps_link, location_type, submitted_by, total_visited, thumbnail, regency_id, is_deleted, created_at, updated_at, approved_by, approved_at FROM locations ` func (q *Queries) GetListLocations(ctx context.Context) ([]Location, error) { @@ -59,6 +59,7 @@ func (q *Queries) GetListLocations(ctx context.Context) ([]Location, error) { &i.Address, &i.Name, &i.GoogleMapsLink, + &i.LocationType, &i.SubmittedBy, &i.TotalVisited, &i.Thumbnail, diff --git a/db/sqlc/models.go b/db/sqlc/models.go index 9d733bc..488ee4d 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -57,6 +57,51 @@ func (ns NullCommentType) Value() (driver.Value, error) { return string(ns.CommentType), nil } +type LocationType string + +const ( + LocationTypeBeach LocationType = "beach" + LocationTypeAmusementpark LocationType = "amusement park" + LocationTypeCulinary LocationType = "culinary" + LocationTypeHikingCamping LocationType = "hiking / camping" + LocationTypeOther LocationType = "other" +) + +func (e *LocationType) Scan(src interface{}) error { + switch s := src.(type) { + case []byte: + *e = LocationType(s) + case string: + *e = LocationType(s) + default: + return fmt.Errorf("unsupported scan type for LocationType: %T", src) + } + return nil +} + +type NullLocationType struct { + LocationType LocationType `json:"location_type"` + Valid bool `json:"valid"` // Valid is true if LocationType is not NULL +} + +// Scan implements the Scanner interface. +func (ns *NullLocationType) Scan(value interface{}) error { + if value == nil { + ns.LocationType, ns.Valid = "", false + return nil + } + ns.Valid = true + return ns.LocationType.Scan(value) +} + +// Value implements the driver Valuer interface. +func (ns NullLocationType) Value() (driver.Value, error) { + if !ns.Valid { + return nil, nil + } + return string(ns.LocationType), nil +} + type UserReportsType string const ( @@ -140,6 +185,7 @@ type Location struct { Address string `json:"address"` Name string `json:"name"` GoogleMapsLink sql.NullString `json:"google_maps_link"` + LocationType LocationType `json:"location_type"` SubmittedBy int32 `json:"submitted_by"` TotalVisited sql.NullInt32 `json:"total_visited"` Thumbnail sql.NullString `json:"thumbnail"` @@ -161,38 +207,39 @@ type LocationImage struct { } type Province struct { - ID int32 `json:"id"` - ProvinceName sql.NullString `json:"province_name"` - RegionID int16 `json:"region_id"` - CreatedAt sql.NullTime `json:"created_at"` - UpdatedAt sql.NullTime `json:"updated_at"` + ID int32 `json:"id"` + ProvinceName string `json:"province_name"` + RegionID int16 `json:"region_id"` + CreatedAt sql.NullTime `json:"created_at"` + UpdatedAt sql.NullTime `json:"updated_at"` } type Regency struct { - ID int32 `json:"id"` - RegencyName sql.NullString `json:"regency_name"` - ProvinceID int16 `json:"province_id"` - CreatedAt sql.NullTime `json:"created_at"` - UpdatedAt sql.NullTime `json:"updated_at"` + ID int32 `json:"id"` + RegencyName string `json:"regency_name"` + ProvinceID int16 `json:"province_id"` + CreatedAt sql.NullTime `json:"created_at"` + UpdatedAt sql.NullTime `json:"updated_at"` } type Region struct { - ID int32 `json:"id"` - RegionName sql.NullString `json:"region_name"` - CreatedAt sql.NullTime `json:"created_at"` - UpdatedAt sql.NullTime `json:"updated_at"` + ID int32 `json:"id"` + RegionName string `json:"region_name"` + CreatedAt sql.NullTime `json:"created_at"` + UpdatedAt sql.NullTime `json:"updated_at"` } type Review struct { - ID int32 `json:"id"` - SubmittedBy int32 `json:"submitted_by"` - Comments string `json:"comments"` - Score int16 `json:"score"` - IsFromCritic bool `json:"is_from_critic"` - IsHided bool `json:"is_hided"` - LocationID int32 `json:"location_id"` - CreatedAt sql.NullTime `json:"created_at"` - UpdatedAt sql.NullTime `json:"updated_at"` + ID int32 `json:"id"` + SubmittedBy int32 `json:"submitted_by"` + Comments string `json:"comments"` + Score int16 `json:"score"` + IsFromCritic bool `json:"is_from_critic"` + CostApprox sql.NullInt32 `json:"cost_approx"` + IsHided bool `json:"is_hided"` + LocationID int32 `json:"location_id"` + CreatedAt sql.NullTime `json:"created_at"` + UpdatedAt sql.NullTime `json:"updated_at"` } type Tag struct { diff --git a/db/sqlc/provinces.sql.go b/db/sqlc/provinces.sql.go new file mode 100644 index 0000000..c28f75a --- /dev/null +++ b/db/sqlc/provinces.sql.go @@ -0,0 +1,47 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.20.0 +// source: provinces.sql + +package db + +import ( + "context" +) + +const getListProvinces = `-- name: GetListProvinces :many +SELECT +id, +province_name, +region_id +FROM PROVINCES +` + +type GetListProvincesRow struct { + ID int32 `json:"id"` + ProvinceName string `json:"province_name"` + RegionID int16 `json:"region_id"` +} + +func (q *Queries) GetListProvinces(ctx context.Context) ([]GetListProvincesRow, error) { + rows, err := q.db.QueryContext(ctx, getListProvinces) + if err != nil { + return nil, err + } + defer rows.Close() + items := []GetListProvincesRow{} + for rows.Next() { + var i GetListProvincesRow + if err := rows.Scan(&i.ID, &i.ProvinceName, &i.RegionID); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 1d20c57..ee5e394 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -15,7 +15,10 @@ type Querier interface { CreateUser(ctx context.Context, arg CreateUserParams) (User, error) GetCountImageByLocation(ctx context.Context, imageOf int32) (int64, error) GetListLocations(ctx context.Context) ([]Location, error) + GetListProvinces(ctx context.Context) ([]GetListProvincesRow, error) GetListRecentLocationsWithRatings(ctx context.Context, limit int32) ([]GetListRecentLocationsWithRatingsRow, error) + GetListRegencies(ctx context.Context) ([]GetListRegenciesRow, error) + GetListRegions(ctx context.Context) ([]GetListRegionsRow, error) GetLocationTag(ctx context.Context, targetID int32) ([]string, error) GetSession(ctx context.Context, id int32) (UserSession, error) GetUserReviewByLocation(ctx context.Context, arg GetUserReviewByLocationParams) (GetUserReviewByLocationRow, error) diff --git a/db/sqlc/regencies.sql.go b/db/sqlc/regencies.sql.go new file mode 100644 index 0000000..1878f21 --- /dev/null +++ b/db/sqlc/regencies.sql.go @@ -0,0 +1,47 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.20.0 +// source: regencies.sql + +package db + +import ( + "context" +) + +const getListRegencies = `-- name: GetListRegencies :many +SELECT +id, +regency_name, +province_id +FROM regencies +` + +type GetListRegenciesRow struct { + ID int32 `json:"id"` + RegencyName string `json:"regency_name"` + ProvinceID int16 `json:"province_id"` +} + +func (q *Queries) GetListRegencies(ctx context.Context) ([]GetListRegenciesRow, error) { + rows, err := q.db.QueryContext(ctx, getListRegencies) + if err != nil { + return nil, err + } + defer rows.Close() + items := []GetListRegenciesRow{} + for rows.Next() { + var i GetListRegenciesRow + if err := rows.Scan(&i.ID, &i.RegencyName, &i.ProvinceID); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/db/sqlc/regions.sql.go b/db/sqlc/regions.sql.go new file mode 100644 index 0000000..0298941 --- /dev/null +++ b/db/sqlc/regions.sql.go @@ -0,0 +1,45 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.20.0 +// source: regions.sql + +package db + +import ( + "context" +) + +const getListRegions = `-- name: GetListRegions :many +SELECT +id, +region_name +FROM regions +` + +type GetListRegionsRow struct { + ID int32 `json:"id"` + RegionName string `json:"region_name"` +} + +func (q *Queries) GetListRegions(ctx context.Context) ([]GetListRegionsRow, error) { + rows, err := q.db.QueryContext(ctx, getListRegions) + if err != nil { + return nil, err + } + defer rows.Close() + items := []GetListRegionsRow{} + for rows.Next() { + var i GetListRegionsRow + if err := rows.Scan(&i.ID, &i.RegionName); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +}