Skip to content

Commit

Permalink
Merge pull request #99 from green-ecolution/add-tree-description
Browse files Browse the repository at this point in the history
Add tree description
  • Loading branch information
choffmann authored Oct 12, 2024
2 parents abf395a + 278de04 commit 662c0d3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 64 deletions.
2 changes: 2 additions & 0 deletions internal/entities/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Tree struct {
Latitude float64
Longitude float64
WateringStatus WateringStatus
Description string
}

type TreeCreate struct {
Expand All @@ -28,4 +29,5 @@ type TreeCreate struct {
Number string `validate:"required"`
Latitude float64 `validate:"required,max=90,min=-90"`
Longitude float64 `validate:"required,max=180,min=-180"`
Description string
}
2 changes: 1 addition & 1 deletion internal/server/http/entities/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TreeResponse struct {
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
WateringStatus WateringStatus `json:"watering_status,omitempty"`
// Description string `json:"description,omitempty" validate:"optional"`
Description string `json:"description,omitempty" validate:"optional"`
} // @Name Tree

type TreeListResponse struct {
Expand Down
9 changes: 8 additions & 1 deletion internal/storage/postgres/mapper/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ import (
// goverter:converter
// goverter:extend github.com/green-ecolution/green-ecolution-backend/internal/utils:PgTimestampToTime
// goverter:extend github.com/green-ecolution/green-ecolution-backend/internal/utils:PgTimestampToTimePtr
// goverter:extend MapWateringStatus
// goverter:extend MapWateringStatus StringPtrToString
type InternalTreeRepoMapper interface {
// goverter:ignore Sensor Images TreeCluster
// goverter:map TreeNumber Number
FromSql(*sqlc.Tree) *entities.Tree
FromSqlList([]*sqlc.Tree) []*entities.Tree
}

func StringPtrToString(source *string) string {
if source == nil {
return ""
}
return *source
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE trees
ADD COLUMN description TEXT;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE trees
DROP COLUMN description;
-- +goose StatementEnd
7 changes: 4 additions & 3 deletions internal/storage/postgres/queries/trees.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ SELECT tree_clusters.* FROM tree_clusters JOIN trees ON tree_clusters.id = trees

-- name: CreateTree :one
INSERT INTO trees (
tree_cluster_id, sensor_id, planting_year, species, tree_number, latitude, longitude, readonly, watering_status, geometry
tree_cluster_id, sensor_id, planting_year, species, tree_number, latitude, longitude, readonly, description, watering_status, geometry
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, ST_GeomFromText($10, 4326)
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, ST_GeomFromText($11, 4326)
) RETURNING id;

-- name: UpdateTree :exec
Expand All @@ -37,7 +37,8 @@ UPDATE trees SET
longitude = $8,
readonly = $9,
watering_status = $10,
geometry = ST_GeomFromText($10, 4326)
description = $11,
geometry = ST_GeomFromText($12, 4326)
WHERE id = $1;

-- name: UpdateTreeClusterID :exec
Expand Down
118 changes: 59 additions & 59 deletions internal/storage/postgres/seed/99_demo_data.sql

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions internal/storage/postgres/tree/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func defaultTree() entities.Tree {
Longitude: 0,
Images: nil,
WateringStatus: entities.WateringStatusUnknown,
Description: "",
}
}

Expand Down Expand Up @@ -69,6 +70,7 @@ func (r *TreeRepository) createEntity(ctx context.Context, entity *entities.Tree
Latitude: entity.Latitude,
Longitude: entity.Longitude,
WateringStatus: sqlc.WateringStatus(entity.WateringStatus),
Description: &entity.Description,
}

return r.store.CreateTree(ctx, &args)
Expand Down
6 changes: 6 additions & 0 deletions internal/storage/postgres/tree/trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func WithSpecies(species string) entities.EntityFunc[entities.Tree] {
}
}

func WithDescription(description string) entities.EntityFunc[entities.Tree] {
return func(t *entities.Tree) {
t.Description = description
}
}

func WithReadonly(readonly bool) entities.EntityFunc[entities.Tree] {
return func(t *entities.Tree) {
t.Readonly = readonly
Expand Down
1 change: 1 addition & 0 deletions internal/storage/postgres/tree/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (r *TreeRepository) updateEntity(ctx context.Context, t *entities.Tree) err
TreeNumber: t.Number,
TreeClusterID: treeClusterID,
WateringStatus: sqlc.WateringStatus(t.WateringStatus),
Description: &t.Description,
}

return r.store.UpdateTree(ctx, &args)
Expand Down

0 comments on commit 662c0d3

Please sign in to comment.