Merge branch main into feature-crop-management

This commit is contained in:
Natthapol SERMSARAN 2025-02-14 03:48:42 +07:00
commit e5e89cb2e1
52 changed files with 5140 additions and 2625 deletions

View File

@ -5,27 +5,46 @@ go 1.23.5
require (
github.com/danielgtaylor/huma/v2 v2.28.0
github.com/go-chi/chi/v5 v5.2.1
github.com/go-chi/cors v1.2.1
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.7.2
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
github.com/pressly/goose/v3 v3.24.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
golang.org/x/crypto v0.31.0
)
require (
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -4,22 +4,34 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/danielgtaylor/huma/v2 v2.28.0 h1:W+hIT52MigO73edJNJWXU896uC99xSBWpKoE2PRyybM=
github.com/danielgtaylor/huma/v2 v2.28.0/go.mod h1:67KO0zmYEkR+LVUs8uqrcvf44G1wXiMIu94LV/cH2Ek=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@ -32,36 +44,67 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pressly/goose/v3 v3.24.1 h1:bZmxRco2uy5uu5Ng1MMVEfYsFlrMJI+e/VMXHQ3C4LY=
github.com/pressly/goose/v3 v3.24.1/go.mod h1:rEWreU9uVtt0DHCyLzF9gRcWiiTF/V+528DV+4DORug=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
@ -69,6 +112,10 @@ golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -10,6 +10,7 @@ import (
"github.com/danielgtaylor/huma/v2/adapters/humachi"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/forfarm/backend/internal/domain"
@ -23,6 +24,7 @@ type api struct {
userRepo domain.UserRepository
cropRepo domain.CroplandRepository
farmRepo domain.FarmRepository
}
func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api {
@ -32,6 +34,7 @@ func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api {
// Initialize repositories for users and croplands
userRepository := repository.NewPostgresUser(pool)
croplandRepository := repository.NewPostgresCropland(pool)
farmRepository := repository.NewPostgresFarm(pool)
return &api{
logger: logger,
@ -39,6 +42,7 @@ func NewAPI(ctx context.Context, logger *slog.Logger, pool *pgxpool.Pool) *api {
userRepo: userRepository,
cropRepo: croplandRepository,
farmRepo: farmRepository,
}
}
@ -52,6 +56,17 @@ func (a *api) Routes() *chi.Mux {
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Use(cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"https://*", "http://*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
config := huma.DefaultConfig("ForFarm Public API", "v1.0.0")
api := humachi.New(router, config)
@ -65,6 +80,8 @@ func (a *api) Routes() *chi.Mux {
router.Group(func(r chi.Router) {
// Apply Authentication middleware to the Cropland routes
api.UseMiddleware(m.AuthMiddleware(api))
a.registerHelloRoutes(r, api)
a.registerFarmRoutes(r, api)
})
return router

View File

@ -0,0 +1,138 @@
package api
import (
"context"
"net/http"
"github.com/danielgtaylor/huma/v2"
"github.com/forfarm/backend/internal/domain"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)
func (a *api) registerFarmRoutes(_ chi.Router, api huma.API) {
tags := []string{"farm"}
prefix := "/farm"
huma.Register(api, huma.Operation{
OperationID: "createFarm",
Method: http.MethodPost,
Path: prefix,
Tags: tags,
}, a.createFarmHandler)
huma.Register(api, huma.Operation{
OperationID: "getFarmsByOwner",
Method: http.MethodGet,
Path: prefix + "/owner/{owner_id}",
Tags: tags,
}, a.getFarmsByOwnerHandler)
huma.Register(api, huma.Operation{
OperationID: "getFarmByID",
Method: http.MethodGet,
Path: prefix + "/{farm_id}",
Tags: tags,
}, a.getFarmByIDHandler)
huma.Register(api, huma.Operation{
OperationID: "deleteFarm",
Method: http.MethodDelete,
Path: prefix + "/{farm_id}",
Tags: tags,
}, a.deleteFarmHandler)
}
type CreateFarmInput struct {
Header string `header:"Authorization" required:"true" example:"Bearer token"`
Body struct {
Name string `json:"name"`
Lat []float64 `json:"lat"`
Lon []float64 `json:"lon"`
OwnerID string `json:"owner_id"`
PlantTypes []uuid.UUID `json:"plant_types"`
}
}
type CreateFarmOutput struct {
Body struct {
UUID string `json:"uuid"`
}
}
func (a *api) createFarmHandler(ctx context.Context, input *CreateFarmInput) (*CreateFarmOutput, error) {
farm := &domain.Farm{
Name: input.Body.Name,
Lat: input.Body.Lat,
Lon: input.Body.Lon,
OwnerID: input.Body.OwnerID,
PlantTypes: input.Body.PlantTypes,
}
err := a.farmRepo.CreateOrUpdate(ctx, farm)
if err != nil {
return nil, err
}
return &CreateFarmOutput{Body: struct {
UUID string `json:"uuid"`
}{UUID: farm.UUID}}, nil
}
type GetFarmsByOwnerInput struct {
Header string `header:"Authorization" required:"true" example:"Bearer token"`
OwnerID string `path:"owner_id"`
}
type GetFarmsByOwnerOutput struct {
Body []domain.Farm
}
func (a *api) getFarmsByOwnerHandler(ctx context.Context, input *GetFarmsByOwnerInput) (*GetFarmsByOwnerOutput, error) {
farms, err := a.farmRepo.GetByOwnerID(ctx, input.OwnerID)
if err != nil {
return nil, err
}
return &GetFarmsByOwnerOutput{Body: farms}, nil
}
type GetFarmByIDInput struct {
Header string `header:"Authorization" required:"true" example:"Bearer token"`
FarmID string `path:"farm_id"`
}
type GetFarmByIDOutput struct {
Body domain.Farm
}
func (a *api) getFarmByIDHandler(ctx context.Context, input *GetFarmByIDInput) (*GetFarmByIDOutput, error) {
farm, err := a.farmRepo.GetByID(ctx, input.FarmID)
if err != nil {
return nil, err
}
return &GetFarmByIDOutput{Body: farm}, nil
}
type DeleteFarmInput struct {
Header string `header:"Authorization" required:"true" example:"Bearer token"`
FarmID string `path:"farm_id"`
}
type DeleteFarmOutput struct {
Body struct {
Message string `json:"message"`
}
}
func (a *api) deleteFarmHandler(ctx context.Context, input *DeleteFarmInput) (*DeleteFarmOutput, error) {
err := a.farmRepo.Delete(ctx, input.FarmID)
if err != nil {
return nil, err
}
return &DeleteFarmOutput{Body: struct {
Message string `json:"message"`
}{Message: "Farm deleted successfully"}}, nil
}

View File

@ -11,10 +11,11 @@ import (
"github.com/forfarm/backend/internal/api"
"github.com/forfarm/backend/internal/cmdutil"
"github.com/forfarm/backend/internal/config"
)
func APICmd(ctx context.Context) *cobra.Command {
var port int = 8000
var port int = config.PORT
cmd := &cobra.Command{
Use: "api",
@ -29,6 +30,8 @@ func APICmd(ctx context.Context) *cobra.Command {
}
defer pool.Close()
logger.Info("connected to database")
api := api.NewAPI(ctx, logger, pool)
server := api.Server(port)

View File

@ -2,14 +2,15 @@ package cmd
import (
"context"
"os"
"github.com/forfarm/backend/internal/config"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
)
func Execute(ctx context.Context) int {
_ = godotenv.Load()
config.Load()
rootCmd := &cobra.Command{
Use: "forfarm",
@ -17,7 +18,7 @@ func Execute(ctx context.Context) int {
}
rootCmd.AddCommand(APICmd(ctx))
rootCmd.AddCommand(MigrateCmd(ctx, "pgx", os.Getenv("DATABASE_URL")))
rootCmd.AddCommand(MigrateCmd(ctx, "pgx", config.DATABASE_URL))
if err := rootCmd.Execute(); err != nil {
return 1

View File

@ -3,9 +3,9 @@ package cmdutil
import (
"context"
"fmt"
"os"
"time"
"github.com/forfarm/backend/internal/config"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
@ -15,7 +15,7 @@ func NewDatabasePool(ctx context.Context, maxConns int) (*pgxpool.Pool, error) {
maxConns = 1
}
url := fmt.Sprintf("%s?pool_max_conns=%d&pool_min_conns=%d", os.Getenv("DATABASE_URL"), maxConns, 2)
url := fmt.Sprintf("%s?pool_max_conns=%d&pool_min_conns=%d", config.DATABASE_URL, maxConns, 2)
config, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, err

View File

@ -0,0 +1,50 @@
package config
import (
"log"
"github.com/spf13/viper"
)
var (
PORT int
POSTGRES_USER string
POSTGRES_PASSWORD string
POSTGRES_DB string
DATABASE_URL string
GOOGLE_CLIENT_ID string
GOOGLE_CLIENT_SECRET string
GOOGLE_REDIRECT_URL string
JWT_SECRET_KEY string
)
func Load() {
viper.SetDefault("PORT", 8000)
viper.SetDefault("POSTGRES_USER", "postgres")
viper.SetDefault("POSTGRES_PASSWORD", "@Password123")
viper.SetDefault("POSTGRES_DB", "postgres")
viper.SetDefault("DATABASE_URL", "localhost")
viper.SetDefault("GOOGLE_CLIENT_ID", "google_client_id")
viper.SetDefault("GOOGLE_CLIENT_SECRET", "google_client_secret")
viper.SetDefault("JWT_SECRET_KEY", "jwt_secret_key")
viper.SetDefault("GOOGLE_REDIRECT_URL", "http://localhost:8000/auth/login/google")
viper.SetConfigFile(".env")
viper.AddConfigPath("../../.")
if err := viper.ReadInConfig(); err != nil {
log.Printf("Warning: Could not read config file: %v", err)
}
viper.AutomaticEnv()
PORT = viper.GetInt("PORT")
POSTGRES_USER = viper.GetString("POSTGRES_USER")
POSTGRES_PASSWORD = viper.GetString("POSTGRES_PASSWORD")
POSTGRES_DB = viper.GetString("POSTGRES_DB")
DATABASE_URL = viper.GetString("DATABASE_URL")
GOOGLE_CLIENT_ID = viper.GetString("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = viper.GetString("GOOGLE_CLIENT_SECRET")
GOOGLE_REDIRECT_URL = viper.GetString("GOOGLE_REDIRECT_URL")
JWT_SECRET_KEY = viper.GetString("JWT_SECRET_KEY")
}

View File

@ -2,19 +2,20 @@ package domain
import (
"context"
"time"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/google/uuid"
"time"
)
type Farm struct {
UUID string
Name string
Lat float64
Lon float64
CreatedAt time.Time
UpdatedAt time.Time
OwnerID string
UUID string
Name string
Lat []float64
Lon []float64
CreatedAt time.Time
UpdatedAt time.Time
OwnerID string
PlantTypes []uuid.UUID
}
func (f *Farm) Validate() error {
@ -28,6 +29,7 @@ func (f *Farm) Validate() error {
type FarmRepository interface {
GetByID(context.Context, string) (Farm, error)
GetByOwnerID(context.Context, string) ([]Farm, error)
CreateOrUpdate(context.Context, *Farm) error
Delete(context.Context, string) error
}

View File

@ -2,6 +2,7 @@ package domain
import (
"context"
"errors"
"strings"
"time"
@ -27,7 +28,22 @@ func (u *User) NormalizedUsername() string {
func (u *User) Validate() error {
return validation.ValidateStruct(u,
validation.Field(&u.UUID, validation.Required),
validation.Field(&u.Username, validation.Length(3, 20)),
validation.Field(&u.Username, validation.By(func(value interface{}) error {
if value == nil {
return nil
}
if value == "" {
return nil
}
username, ok := value.(*string)
if !ok {
return errors.New("invalid type for username")
}
if len(*username) < 3 || len(*username) > 20 {
return errors.New("username length must be between 3 and 20")
}
return nil
})),
validation.Field(&u.Password, validation.Required, validation.Length(6, 100)),
validation.Field(&u.Email, validation.Required, is.Email),
)

View File

@ -2,11 +2,10 @@ package repository
import (
"context"
"strings"
"github.com/google/uuid"
"github.com/forfarm/backend/internal/domain"
"github.com/google/uuid"
"github.com/lib/pq"
"strings"
)
type postgresFarmRepository struct {
@ -27,6 +26,7 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
var farms []domain.Farm
for rows.Next() {
var f domain.Farm
var plantTypes pq.StringArray
if err := rows.Scan(
&f.UUID,
&f.Name,
@ -35,9 +35,19 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
&f.CreatedAt,
&f.UpdatedAt,
&f.OwnerID,
&plantTypes,
); err != nil {
return nil, err
}
for _, plantTypeStr := range plantTypes {
plantTypeUUID, err := uuid.Parse(plantTypeStr)
if err != nil {
return nil, err
}
f.PlantTypes = append(f.PlantTypes, plantTypeUUID)
}
farms = append(farms, f)
}
return farms, nil
@ -45,7 +55,7 @@ func (p *postgresFarmRepository) fetch(ctx context.Context, query string, args .
func (p *postgresFarmRepository) GetByID(ctx context.Context, uuid string) (domain.Farm, error) {
query := `
SELECT uuid, name, lat, lon, created_at, updated_at, owner_id
SELECT uuid, name, lat, lon, created_at, updated_at, owner_id, plant_types
FROM farms
WHERE uuid = $1`
@ -61,7 +71,7 @@ func (p *postgresFarmRepository) GetByID(ctx context.Context, uuid string) (doma
func (p *postgresFarmRepository) GetByOwnerID(ctx context.Context, ownerID string) ([]domain.Farm, error) {
query := `
SELECT uuid, name, lat, lon, created_at, updated_at, owner_id
SELECT uuid, name, lat, lon, created_at, updated_at, owner_id, plant_types
FROM farms
WHERE owner_id = $1`
@ -73,15 +83,21 @@ func (p *postgresFarmRepository) CreateOrUpdate(ctx context.Context, f *domain.F
f.UUID = uuid.New().String()
}
plantTypes := make([]string, len(f.PlantTypes))
for i, pt := range f.PlantTypes {
plantTypes[i] = pt.String()
}
query := `
INSERT INTO farms (uuid, name, lat, lon, created_at, updated_at, owner_id)
VALUES ($1, $2, $3, $4, NOW(), NOW(), $5)
INSERT INTO farms (uuid, name, lat, lon, created_at, updated_at, owner_id, plant_types)
VALUES ($1, $2, $3, $4, NOW(), NOW(), $5, $6)
ON CONFLICT (uuid) DO UPDATE
SET name = EXCLUDED.name,
lat = EXCLUDED.lat,
lon = EXCLUDED.lon,
updated_at = NOW(),
owner_id = EXCLUDED.owner_id
owner_id = EXCLUDED.owner_id,
plant_types = EXCLUDED.plant_types
RETURNING uuid, created_at, updated_at`
return p.conn.QueryRow(
@ -92,7 +108,8 @@ func (p *postgresFarmRepository) CreateOrUpdate(ctx context.Context, f *domain.F
f.Lat,
f.Lon,
f.OwnerID,
).Scan(&f.CreatedAt, &f.UpdatedAt)
pq.StringArray(plantTypes),
).Scan(&f.UUID, &f.CreatedAt, &f.UpdatedAt)
}
func (p *postgresFarmRepository) Delete(ctx context.Context, uuid string) error {

View File

@ -1,13 +1,15 @@
package utilities
import (
"errors"
"time"
"github.com/forfarm/backend/internal/config"
"github.com/golang-jwt/jwt/v5"
)
// TODO: Change later
var secretKey = []byte("secret-key")
var deafultSecretKey = []byte(config.JWT_SECRET_KEY)
func CreateJwtToken(uuid string) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
@ -15,7 +17,7 @@ func CreateJwtToken(uuid string) (string, error) {
"exp": time.Now().Add(time.Hour * 24).Unix(),
})
tokenString, err := token.SignedString(secretKey)
tokenString, err := token.SignedString(deafultSecretKey)
if err != nil {
return "", err
}
@ -23,13 +25,20 @@ func CreateJwtToken(uuid string) (string, error) {
return tokenString, nil
}
func VerifyJwtToken(tokenString string) error {
func VerifyJwtToken(tokenString string, customKey ...[]byte) error {
secretKey := deafultSecretKey
if len(customKey) > 0 {
if len(customKey[0]) < 32 {
return errors.New("provided key is too short, minimum length is 32 bytes")
}
secretKey = customKey[0]
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
// TODO: CHANGE SECRET KEY
return secretKey, nil
})

View File

@ -10,5 +10,4 @@ CREATE TABLE users (
is_active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE UNIQUE INDEX idx_users_uuid ON users(uuid);
CREATE UNIQUE INDEX idx_users_username ON users(username);
CREATE UNIQUE INDEX idx_users_uuid ON users(uuid);

View File

@ -43,8 +43,9 @@ CREATE TABLE plants (
CREATE TABLE farms (
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
lat DOUBLE PRECISION NOT NULL,
lon DOUBLE PRECISION NOT NULL,
lat DOUBLE PRECISION[] NOT NULL,
lon DOUBLE PRECISION[] NOT NULL,
plant_types UUID[],
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
owner_id UUID NOT NULL,

View File

@ -0,0 +1,48 @@
import axios from "axios";
import axiosInstance from "./config";
export interface LoginResponse {
token: string;
message?: string;
}
export interface RegisterResponse {
token: string;
message?: string;
}
/**
* Registers a new user by sending a POST request to the backend.
*/
export async function registerUser(email: string, password: string): Promise<RegisterResponse> {
try {
const response = await axiosInstance.post("/auth/register", {
Email: email,
Password: password,
});
return response.data;
} catch (error: any) {
if (axios.isAxiosError(error)) {
throw new Error(error.response?.data?.message || "Failed to register.");
}
throw error;
}
}
/**
* Logs in a user by sending a POST request to the backend.
*/
export async function loginUser(email: string, password: string): Promise<LoginResponse> {
try {
const response = await axiosInstance.post("/auth/login", {
Email: email,
Password: password,
});
return response.data;
} catch (error: any) {
if (axios.isAxiosError(error)) {
throw new Error(error.response?.data?.message || "Failed to log in.");
}
throw error;
}
}

28
frontend/api/config.ts Normal file
View File

@ -0,0 +1,28 @@
import axios from "axios";
const axiosInstance = axios.create({
baseURL: process.env.NEXT_PUBLIC_BACKEND_URL || "http://localhost:8000",
headers: {
"Content-Type": "application/json",
},
});
axiosInstance.interceptors.request.use(
(config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);
axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
return Promise.reject(error);
}
);
export default axiosInstance;

View File

@ -0,0 +1,98 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Crop } from "@/types";
import { cropFormSchema } from "@/schemas/form.schema";
interface AddCropFormProps {
onSubmit: (data: Partial<Crop>) => Promise<void>;
onCancel: () => void;
}
export function AddCropForm({ onSubmit, onCancel }: AddCropFormProps) {
const form = useForm<z.infer<typeof cropFormSchema>>({
resolver: zodResolver(cropFormSchema),
defaultValues: {
name: "",
plantedDate: "",
status: "planned",
},
});
const handleSubmit = (values: z.infer<typeof cropFormSchema>) => {
onSubmit({
...values,
plantedDate: new Date(values.plantedDate),
});
};
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-6">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Crop Name</FormLabel>
<FormControl>
<Input placeholder="Enter crop name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="plantedDate"
render={({ field }) => (
<FormItem>
<FormLabel>Planted Date</FormLabel>
<FormControl>
<Input type="date" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="status"
render={({ field }) => (
<FormItem>
<FormLabel>Status</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select crop status" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="planned">Planned</SelectItem>
<SelectItem value="growing">Growing</SelectItem>
<SelectItem value="harvested">Harvested</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end gap-2">
<Button type="button" variant="outline" onClick={onCancel}>
Cancel
</Button>
<Button type="submit">Add Crop</Button>
</div>
</form>
</Form>
);
}

View File

@ -0,0 +1,37 @@
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Sprout, Calendar } from "lucide-react";
import { Crop } from "@/types";
interface CropCardProps {
crop: Crop;
}
export function CropCard({ crop }: CropCardProps) {
const statusColors = {
growing: "text-green-500",
harvested: "text-yellow-500",
planned: "text-blue-500",
};
return (
<Card className="w-full bg-muted/50 hover:bg-muted/80 transition-all cursor-pointer group hover:shadow-lg">
<CardHeader className="p-4 pb-2">
<div className="flex items-center justify-between">
<div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center">
<Sprout className="h-4 w-4 text-primary" />
</div>
<span className={`text-sm font-medium capitalize ${statusColors[crop.status]}`}>{crop.status}</span>
</div>
</CardHeader>
<CardContent className="p-4">
<div className="space-y-2">
<h3 className="text-lg font-medium truncate">{crop.name}</h3>
<div className="flex items-center gap-1 text-sm text-muted-foreground">
<Calendar className="h-3 w-3" />
<p>Planted: {crop.plantedDate.toLocaleDateString()}</p>
</div>
</div>
</CardContent>
</Card>
);
}

View File

@ -0,0 +1,157 @@
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { ArrowLeft, MapPin, Plus, Sprout } from "lucide-react";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog";
import { AddCropForm } from "./add-crop-form";
import { CropCard } from "./crop-card";
import { Farm, Crop } from "@/types";
import React from "react";
const crops: Crop[] = [
{
id: "crop1",
farmId: "1",
name: "Monthong Durian",
plantedDate: new Date("2023-03-15"),
status: "growing",
},
{
id: "crop2",
farmId: "1",
name: "Chanee Durian",
plantedDate: new Date("2023-02-20"),
status: "planned",
},
{
id: "crop3",
farmId: "2",
name: "Kradum Durian",
plantedDate: new Date("2022-11-05"),
status: "harvested",
},
];
const farms: Farm[] = [
{
id: "1",
name: "Green Valley Farm",
location: "Bangkok",
type: "durian",
createdAt: new Date("2023-01-01"),
},
{
id: "2",
name: "Golden Farm",
location: "Chiang Mai",
type: "mango",
createdAt: new Date("2022-12-01"),
},
];
const getFarmById = (id: string): Farm | undefined => {
return farms.find((farm) => farm.id === id);
};
const getCropsByFarmId = (farmId: string): Crop[] => crops.filter((crop) => crop.farmId === farmId);
export default function FarmDetailPage({ params }: { params: Promise<{ farmId: string }> }) {
const { farmId } = React.use(params);
const router = useRouter();
const [farm] = useState<Farm | undefined>(getFarmById(farmId));
const [crops, setCrops] = useState<Crop[]>(getCropsByFarmId(farmId));
const [isDialogOpen, setIsDialogOpen] = useState(false);
const handleAddCrop = async (data: Partial<Crop>) => {
const newCrop: Crop = {
id: Math.random().toString(36).substr(2, 9),
farmId: farm!.id,
name: data.name!,
plantedDate: data.plantedDate!,
status: data.status!,
};
setCrops((prevCrops) => [...prevCrops, newCrop]);
setIsDialogOpen(false);
};
return (
<div className="container max-w-screen-xl p-8">
<Button variant="ghost" className="mb-4" onClick={() => router.back()}>
<ArrowLeft className="mr-2 h-4 w-4" /> Back to Farms
</Button>
<div className="grid gap-6 md:grid-cols-2">
<Card className="md:col-span-2">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<div className="flex items-center space-x-2">
<div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center">
<Sprout className="h-4 w-4 text-primary" />
</div>
<h1 className="text-2xl font-bold">{farm?.name ?? "Unknown Farm"}</h1>
</div>
<div className="flex items-center text-sm text-muted-foreground">
<MapPin className="mr-1 h-4 w-4" />
{farm?.location ?? "Unknown Location"}
</div>
</CardHeader>
<CardContent>
<div className="grid gap-2">
<div className="flex justify-between">
<span className="font-medium">Farm Type:</span>
<span className="text-muted-foreground">{farm?.type ?? "Unknown Type"}</span>
</div>
<div className="flex justify-between">
<span className="font-medium">Created:</span>
<span className="text-muted-foreground">{farm?.createdAt?.toLocaleDateString() ?? "Unknown Date"}</span>
</div>
<div className="flex justify-between">
<span className="font-medium">Total Crops:</span>
<span className="text-muted-foreground">{crops.length}</span>
</div>
</div>
</CardContent>
</Card>
<div className="md:col-span-2">
<h2 className="text-xl font-bold mb-4">Crops</h2>
<Separator className="my-4" />
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<Card
className="w-full bg-muted/50 hover:bg-muted/80 transition-all cursor-pointer group hover:shadow-lg"
onClick={() => setIsDialogOpen(true)}>
<CardContent className="p-6">
<div className="flex flex-col gap-6">
<div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center group-hover:bg-primary/20 transition-colors">
<Plus className="h-5 w-5 text-primary" />
</div>
<div className="space-y-1">
<h3 className="text-xl font-medium">Add Crop</h3>
<p className="text-sm text-muted-foreground">Plant a new crop</p>
</div>
</div>
</CardContent>
</Card>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Add New Crop</DialogTitle>
<DialogDescription>Fill out the form to add a new crop to your farm.</DialogDescription>
</DialogHeader>
<AddCropForm onSubmit={handleAddCrop} onCancel={() => setIsDialogOpen(false)} />
</DialogContent>
</Dialog>
{crops.map((crop) => (
<CropCard key={crop.id} crop={crop} />
))}
</div>
</div>
</div>
</div>
);
}

View File

@ -0,0 +1,93 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import type { Farm } from "@/types";
import { farmFormSchema } from "@/schemas/form.schema";
interface AddFarmFormProps {
onSubmit: (data: Partial<Farm>) => Promise<void>;
onCancel: () => void;
}
export function AddFarmForm({ onSubmit, onCancel }: AddFarmFormProps) {
const form = useForm<z.infer<typeof farmFormSchema>>({
resolver: zodResolver(farmFormSchema),
defaultValues: {
name: "",
location: "",
type: "",
},
});
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Farm Name</FormLabel>
<FormControl>
<Input placeholder="Enter farm name" {...field} />
</FormControl>
<FormDescription>This is your farm&apos;s display name.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="location"
render={({ field }) => (
<FormItem>
<FormLabel>Location</FormLabel>
<FormControl>
<Input placeholder="Enter farm location" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="type"
render={({ field }) => (
<FormItem>
<FormLabel>Farm Type</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select farm type" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="durian">Durian</SelectItem>
<SelectItem value="mango">Mango</SelectItem>
<SelectItem value="rice">Rice</SelectItem>
<SelectItem value="other">Other</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end gap-2">
<Button type="button" variant="outline" onClick={onCancel}>
Cancel
</Button>
<Button type="submit">Create Farm</Button>
</div>
</form>
</Form>
);
}

View File

@ -0,0 +1,61 @@
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { MapPin, Sprout, Plus } from "lucide-react";
import type { Farm } from "@/types";
export interface FarmCardProps {
variant: "farm" | "add";
farm?: Farm;
onClick?: () => void;
}
export function FarmCard({ variant, farm, onClick }: FarmCardProps) {
const cardClasses =
"w-full max-w-[240px] bg-muted/50 hover:bg-muted/80 transition-all cursor-pointer group hover:shadow-lg";
if (variant === "add") {
return (
<Card className={cardClasses} onClick={onClick}>
<CardContent className="p-6">
<div className="flex flex-col gap-6">
<div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center group-hover:bg-primary/20 transition-colors">
<Plus className="h-5 w-5 text-primary" />
</div>
<div className="space-y-1">
<h3 className="text-xl font-medium">Setup</h3>
<p className="text-sm text-muted-foreground">Setup new farm</p>
</div>
</div>
</CardContent>
</Card>
);
}
if (variant === "farm" && farm) {
return (
<Card className={cardClasses} onClick={onClick}>
<CardHeader className="p-4 pb-0">
<div className="flex items-center justify-between">
<div className="h-8 w-8 rounded-full bg-primary/10 flex items-center justify-center">
<Sprout className="h-4 w-4 text-primary" />
</div>
<span className="text-sm font-medium text-primary">{farm.type}</span>
</div>
</CardHeader>
<CardContent className="p-4">
<div className="space-y-4">
<div>
<h3 className="text-xl font-medium truncate">{farm.name}</h3>
<div className="flex items-center gap-1 mt-1 text-muted-foreground">
<MapPin className="h-3 w-3" />
<p className="text-sm">{farm.location}</p>
</div>
</div>
<div className="text-xs text-muted-foreground">Created {farm.createdAt.toLocaleDateString()}</div>
</div>
</CardContent>
</Card>
);
}
return null;
}

View File

@ -0,0 +1,78 @@
"use client";
import { useState } from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog";
import { Separator } from "@/components/ui/separator";
import { Input } from "@/components/ui/input";
import { Search } from "lucide-react";
import { FarmCard } from "./farm-card";
import { AddFarmForm } from "./add-farm-form";
import type { Farm } from "@/types";
export default function FarmSetupPage() {
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [searchQuery, setSearchQuery] = useState("");
const [farms, setFarms] = useState<Farm[]>([
{
id: "1",
name: "Green Valley Farm",
location: "Bangkok",
type: "durian",
createdAt: new Date(),
},
]);
const handleAddFarm = async (data: Partial<Farm>) => {
const newFarm: Farm = {
id: Math.random().toString(36).substr(2, 9),
name: data.name!,
location: data.location!,
type: data.type!,
createdAt: new Date(),
};
setFarms([...farms, newFarm]);
setIsDialogOpen(false);
};
const filteredFarms = farms.filter(
(farm) =>
farm.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
farm.location.toLowerCase().includes(searchQuery.toLowerCase()) ||
farm.type.toLowerCase().includes(searchQuery.toLowerCase())
);
return (
<div className="container max-w-screen-xl p-8">
<div className="flex justify-between items-center">
<h1 className="text-3xl font-bold">Farms</h1>
<div className="relative w-64">
<Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search farms..."
className="pl-8"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
/>
</div>
</div>
<Separator className="my-4" />
<div className="grid grid-cols-5 gap-4">
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<FarmCard variant="add" onClick={() => setIsDialogOpen(true)} />
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Setup New Farm</DialogTitle>
<DialogDescription>Fill out the form to configure your new farm.</DialogDescription>
</DialogHeader>
<AddFarmForm onSubmit={handleAddFarm} onCancel={() => setIsDialogOpen(false)} />
</DialogContent>
</Dialog>
{filteredFarms.map((farm) => (
<FarmCard key={farm.id} variant="farm" farm={farm} />
))}
</div>
</div>
);
}

View File

@ -1,4 +1,5 @@
import { AppSidebar } from "@/components/app-sidebar";
import { AppSidebar } from "@/components/sidebar/app-sidebar";
import { ThemeToggle } from "@/components/theme-toggle";
import {
Breadcrumb,
BreadcrumbItem,
@ -22,6 +23,7 @@ export default function AppLayout({
<header className="flex h-16 shrink-0 items-center gap-2">
<div className="flex items-center gap-2 px-4">
<SidebarTrigger className="-ml-1" />
<ThemeToggle />
<Separator orientation="vertical" className="mr-2 h-4" />
<Breadcrumb>
<BreadcrumbList>

View File

@ -14,24 +14,13 @@ const GoogleMapWithDrawing = () => {
const [map, setMap] = useState<google.maps.Map | null>(null);
// Handles drawing complete
const onDrawingComplete = useCallback(
(overlay: google.maps.drawing.OverlayCompleteEvent) => {
console.log("Drawing complete:", overlay);
},
[]
);
const onDrawingComplete = useCallback((overlay: google.maps.drawing.OverlayCompleteEvent) => {
console.log("Drawing complete:", overlay);
}, []);
return (
<LoadScript
googleMapsApiKey={process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY!}
libraries={["drawing"]}
>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
onLoad={(map) => setMap(map)}
>
<LoadScript googleMapsApiKey={process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY!} libraries={["drawing"]}>
<GoogleMap mapContainerStyle={containerStyle} center={center} zoom={10} onLoad={(map) => setMap(map)}>
{map && (
<DrawingManager
onOverlayComplete={onDrawingComplete}

View File

@ -0,0 +1,9 @@
import Image from "next/image";
export function GoogleSigninButton() {
return (
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer ">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
</div>
);
}

View File

@ -1,3 +1,10 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { signInSchema } from "@/schemas/auth.schema";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
@ -6,19 +13,65 @@ import ForgotPasswordModal from "./forgot-password-modal";
import Link from "next/link";
import Image from "next/image";
import { GoogleSigninButton } from "./google-oauth";
import { z } from "zod";
import { useContext, useState } from "react";
import { useRouter } from "next/navigation";
import { loginUser } from "@/api/authentication";
import { SessionContext } from "@/context/SessionContext";
export default function SigninPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const session = useContext(SessionContext);
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
resolver: zodResolver(signInSchema),
defaultValues: {
email: "",
password: "",
},
});
const onSubmit = async (values: z.infer<typeof signInSchema>) => {
setServerError(null);
setIsLoading(true);
try {
const data = await loginUser(values.email, values.password);
if (!data) {
setServerError("An error occurred while logging in. Please try again.");
throw new Error("No data received from the server.");
}
session!.setToken(data.token);
session!.setUser(values.email);
router.push("/setup");
} catch (error: any) {
console.error("Error logging in:", error);
setServerError(error.message);
} finally {
setIsLoading(false);
}
};
return (
<div>
<div className="grid grid-cols-[0.7fr_1.2fr] h-screen overflow-hidden">
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<div className="flex justify-center items-center">
{/* login box */}
<div className="container px-[25%]">
<div className="flex flex-col justify-center items-center">
<span>
<Image src={`/forfarm-logo.png`} alt="Forfarm" width={150} height={150}></Image>
<Image src="/forfarm-logo.png" alt="Forfarm" width={150} height={150} />
</span>
<h1 className="text-3xl font-semibold">Welcome back.</h1>
<div className="flex whitespace-nowrap gap-x-2 mt-2">
@ -31,27 +84,28 @@ export default function SigninPage() {
</div>
</div>
<div className="flex flex-col mt-4">
{/* Sign in form */}
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col mt-4">
<div>
<Label htmlFor="email">Email</Label>
<Input type="email" id="email" placeholder="Email" />
<Input type="email" id="email" placeholder="Email" {...register("email")} />
{errors.email && <p className="text-red-600 text-sm">{errors.email.message}</p>}
</div>
<div className="mt-5">
<div>
<Label htmlFor="password">Password</Label>
<Input type="empasswordail" id="password" placeholder="Password" />
</div>
<Label htmlFor="password">Password</Label>
<Input type="password" id="password" placeholder="Password" {...register("password")} />
{errors.password && <p className="text-red-600 text-sm">{errors.password.message}</p>}
</div>
<Button className="mt-5 rounded-full">Log in</Button>
</div>
<Button type="submit" className="mt-5 rounded-full" disabled={isLoading}>
{isLoading ? "Logging in..." : "Log in"}
</Button>
</form>
<div id="signin-footer" className="flex justify-between mt-5">
<div className="flex items-center space-x-2">
<Checkbox id="terms" />
<label
htmlFor="terms"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
<label htmlFor="terms" className="text-sm font-medium leading-none">
Remember me
</label>
</div>
@ -60,12 +114,8 @@ export default function SigninPage() {
<div className="my-5">
<p className="text-sm">Or log in with</p>
{/* OAUTH */}
<div className="flex flex-col gap-x-5 mt-3">
{/* Google */}
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer ">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
</div>
<GoogleSigninButton />
</div>
</div>
</div>

View File

@ -1,62 +1,128 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
import { Button } from "@/components/ui/button";
import { signUpSchema } from "@/schemas/auth.schema";
import Link from "next/link";
import Image from "next/image";
import { useContext, useState } from "react";
import { z } from "zod";
import { useRouter } from "next/navigation";
import { registerUser } from "@/api/authentication";
import { SessionContext } from "@/context/SessionContext";
export default function SignupPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [successMessage, setSuccessMessage] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const session = useContext(SessionContext);
const {
register,
handleSubmit,
formState: { errors },
} = useForm<z.infer<typeof signUpSchema>>({
resolver: zodResolver(signUpSchema),
defaultValues: {
email: "",
password: "",
confirmPassword: "",
},
});
const onSubmit = async (values: z.infer<typeof signUpSchema>) => {
setServerError(null);
setSuccessMessage(null);
setIsLoading(true);
try {
const data = await registerUser(values.email, values.password);
if (!data) {
setServerError("An error occurred while registering. Please try again.");
throw new Error("No data received from the server.");
}
session!.setToken(data.token);
session!.setUser(values.email);
setSuccessMessage("Registration successful! You can now sign in.");
router.push("/setup");
} catch (error: any) {
console.error("Error during registration:", error);
setServerError(error.message);
} finally {
setIsLoading(false);
}
};
return (
<div>
<div className="grid grid-cols-[0.7fr_1.2fr] h-screen overflow-hidden">
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<div className="flex bg-[url('/plant-background.jpeg')] bg-cover bg-center"></div>
<div className="flex justify-center items-center">
{/* login box */}
<div className="container px-[25%]">
<div className="flex flex-col justify-center items-center">
<span>
<Image src={`/forfarm-logo.png`} alt="Forfarm" width={150} height={150}></Image>
<Image src="/forfarm-logo.png" alt="Forfarm" width={150} height={150} />
</span>
<h1 className="text-3xl font-semibold">Hi! Welcome</h1>
<div className="flex whitespace-nowrap gap-x-2 mt-2">
<span className="text-md">Already have accounts?</span>
<span className="text-md">Already have an account?</span>
<span className="text-green-600">
<Link href="signin" className="underline">
<Link href="/auth/signin" className="underline">
Sign in
</Link>
</span>
</div>
</div>
<div className="flex flex-col mt-4">
{/* Signup form */}
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col mt-4">
<div>
<Label htmlFor="email">Email</Label>
<Input type="email" id="email" placeholder="Email" />
<Input type="email" id="email" placeholder="Email" {...register("email")} />
{errors.email && <p className="text-red-600 text-sm">{errors.email.message}</p>}
</div>
<div className="mt-5">
<div>
<Label htmlFor="password">Password</Label>
<Input type="empasswordail" id="password" placeholder="Password" />
</div>
<Label htmlFor="password">Password</Label>
<Input type="password" id="password" placeholder="Password" {...register("password")} />
{errors.password && <p className="text-red-600 text-sm">{errors.password.message}</p>}
</div>
<div className="mt-5">
<div>
<Label htmlFor="password">Confirm Password</Label>
<Input type="empasswordail" id="password" placeholder="Password" />
</div>
<Label htmlFor="confirmPassword">Confirm Password</Label>
<Input
type="password"
id="confirmPassword"
placeholder="Confirm Password"
{...register("confirmPassword")}
/>
{errors.confirmPassword && <p className="text-red-600 text-sm">{errors.confirmPassword.message}</p>}
</div>
<Button className="mt-5 rounded-full">Sign up</Button>
</div>
{serverError && <p className="text-red-600 mt-2 text-sm">{serverError}</p>}
{successMessage && <p className="text-green-600 mt-2 text-sm">{successMessage}</p>}
<Button type="submit" className="mt-5 rounded-full" disabled={isLoading}>
{isLoading ? "Signing up..." : "Sign up"}
</Button>
</form>
<div className="my-5">
<p className="text-sm">Or log in with</p>
{/* OAUTH */}
<p className="text-sm">Or sign up with</p>
<div className="flex flex-col gap-x-5 mt-3">
{/* Google */}
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer ">
{/* Google OAuth button or additional providers */}
<div className="flex w-1/3 justify-center rounded-full border-2 border-border bg-gray-100 hover:bg-gray-300 duration-300 cursor-pointer">
<Image src="/google-logo.png" alt="Google Logo" width={35} height={35} className="object-contain" />
</div>
</div>

View File

@ -3,6 +3,8 @@ import { Open_Sans, Roboto_Mono } from "next/font/google";
import "./globals.css";
import { ThemeProvider } from "@/components/theme-provider";
import { SessionProvider } from "@/context/SessionContext";
const openSans = Open_Sans({
subsets: ["latin"],
display: "swap",
@ -33,13 +35,15 @@ export default function RootLayout({
return (
<html lang="en" suppressHydrationWarning>
<head />
<body className={`${openSans.variable} ${robotoMono.variable} font-sans antialiased`}>
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<div className="relative flex min-h-screen flex-col">
<div className="flex-1 bg-background">{children}</div>
</div>
</ThemeProvider>
</body>
<SessionProvider>
<body className={`${openSans.variable} ${robotoMono.variable} antialiased`}>
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
<div className="relative flex min-h-screen flex-col">
<div className="flex-1 bg-background">{children}</div>
</div>
</ThemeProvider>
</body>
</SessionProvider>
</html>
);
}

View File

@ -41,7 +41,7 @@ export default function Home() {
It's a smart and easy way to optimize your agricultural business, with the help of AI-driven insights and
real-time data.
</p>
<Link href="/auth/signin">
<Link href="/setup">
<Button className="bg-black text-white text-md font-bold px-4 py-6 rounded-full hover:bg-gray-600">
Manage your farm
</Button>

View File

@ -0,0 +1,12 @@
"use client";
import React, { ReactNode } from "react";
import { SessionProvider } from "next-auth/react";
interface Props {
children: ReactNode;
}
export function SessesionProviderClient(props: Props) {
return <SessionProvider>{props.children}</SessionProvider>;
}

View File

@ -1,144 +0,0 @@
"use client";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
} from "@/components/ui/sidebar";
import {
Calendar,
Home,
Settings,
Sun,
Moon,
LogOut,
Wrench,
FileText,
Bot,
Factory,
Store,
} from "lucide-react";
import { usePathname } from "next/navigation";
import { useState } from "react";
import Image from "next/image";
export function AppSidebar() {
const pathname = usePathname();
const [darkMode, setDarkMode] = useState(false);
const items = [
{
title: "Dashboard",
url: "#",
icon: Home,
},
{
title: "SetUp",
url: "#",
icon: Wrench,
},
{
title: "Management",
url: "#",
icon: Calendar,
},
{
title: "Work Order Management",
url: "#",
icon: FileText,
},
{
title: "AI-Chatbot",
url: "#",
icon: Bot,
},
{
title: "Inventory Management",
url: "#",
icon: Factory,
},
{
title: "Marketplace",
url: "#",
icon: Store,
},
{
title: "Settings",
url: "#",
icon: Settings,
},
];
return (
<Sidebar className="w-64 h-screen bg-gray-100 border-r border-gray-300 shadow-md flex flex-col justify-between">
{/* Menu Items */}
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel className="text-lg font-semibold text-gray-700 px-4 py-3">
<div className="flex items-center gap-x-2 mt-14">
<Image
src="/forfarm-logo.png"
width={80}
height={80}
alt="ForFarm Logo"
className="w-24 h-24 rounded-full"
/>
<h1 className="text-xl">ForFarm</h1>
</div>
</SidebarGroupLabel>
<SidebarGroupContent className="flex flex-col flex-grow justify-center mt-24">
<SidebarMenu>
{items.map((item) => {
const isActive = pathname === item.url;
return (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton asChild>
<a
href={item.url}
className={`flex h-14 items-center gap-3 my-2 px-4 py-3 rounded-lg text-gray-700 transition duration-300 ${
isActive
? "bg-blue-500 text-white font-semibold"
: "hover:bg-gray-200"
}`}
>
<item.icon />
<span>{item.title}</span>
</a>
</SidebarMenuButton>
</SidebarMenuItem>
);
})}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
{/* Bottom Section: Theme Toggle & Logout */}
<div className="p-4 border-t border-gray-300">
{/* Theme Toggle */}
<button
className="flex items-center gap-3 w-full px-4 py-3 rounded-lg transition duration-300 hover:bg-gray-200"
onClick={() => setDarkMode(!darkMode)}
>
{darkMode ? <Sun size={18} /> : <Moon size={18} />}
<span>{darkMode ? "Light Mode" : "Dark Mode"}</span>
</button>
{/* Logout Button */}
<button
className="flex items-center gap-3 w-full px-4 py-3 rounded-lg text-red-500 transition duration-300 hover:bg-red-100 mt-2"
onClick={() => alert("Logging out...")} // Replace with actual logout function
>
<LogOut size={18} />
<span>Logout</span>
</button>
</div>
</Sidebar>
);
}

View File

@ -0,0 +1,152 @@
"use client";
import * as React from "react";
import {
AudioWaveform,
BookOpen,
Bot,
Command,
Frame,
GalleryVerticalEnd,
Map,
PieChart,
Settings2,
SquareTerminal,
} from "lucide-react";
import { NavMain } from "./nav-main";
import { NavProjects } from "./nav-projects";
import { NavUser } from "./nav-user";
import { TeamSwitcher } from "./team-switcher";
import { Sidebar, SidebarContent, SidebarFooter, SidebarHeader, SidebarRail } from "@/components/ui/sidebar";
const data = {
user: {
name: "shadcn",
email: "m@example.com",
avatar: "/avatars/shadcn.jpg",
},
teams: [
{
name: "Farm 1",
logo: GalleryVerticalEnd,
plan: "Hatyai",
},
{
name: "Farm 2",
logo: AudioWaveform,
plan: "Songkla",
},
{
name: "Farm 3",
logo: Command,
plan: "Layong",
},
],
navMain: [
{
title: "Dashboard",
url: "#",
icon: SquareTerminal,
isActive: true,
items: [
{
title: "Analytic",
url: "#",
},
],
},
{
title: "AI Chatbot",
url: "#",
icon: Bot,
items: [
{
title: "Main model",
url: "#",
},
],
},
{
title: "Documentation",
url: "#",
icon: BookOpen,
items: [
{
title: "Introduction",
url: "#",
},
{
title: "Get Started",
url: "#",
},
{
title: "Tutorials",
url: "#",
},
{
title: "Changelog",
url: "#",
},
],
},
{
title: "Settings",
url: "#",
icon: Settings2,
items: [
{
title: "General",
url: "#",
},
{
title: "Team",
url: "#",
},
{
title: "Billing",
url: "#",
},
{
title: "Limits",
url: "#",
},
],
},
],
projects: [
{
name: "Crops 1",
url: "#",
icon: Frame,
},
{
name: "Crops 2",
url: "#",
icon: PieChart,
},
{
name: "Crops 3",
url: "#",
icon: Map,
},
],
};
export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
return (
<Sidebar collapsible="icon" {...props}>
<SidebarHeader>
<TeamSwitcher teams={data.teams} />
</SidebarHeader>
<SidebarContent>
<NavMain items={data.navMain} />
<NavProjects projects={data.projects} />
</SidebarContent>
<SidebarFooter>
<NavUser user={data.user} />
</SidebarFooter>
<SidebarRail />
</Sidebar>
);
}

View File

@ -0,0 +1,64 @@
"use client";
import { ChevronRight, type LucideIcon } from "lucide-react";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
import {
SidebarGroup,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
} from "@/components/ui/sidebar";
export function NavMain({
items,
}: {
items: {
title: string;
url: string;
icon?: LucideIcon;
isActive?: boolean;
items?: {
title: string;
url: string;
}[];
}[];
}) {
return (
<SidebarGroup>
<SidebarGroupLabel>Platform</SidebarGroupLabel>
<SidebarMenu>
{items.map((item) => (
<Collapsible key={item.title} asChild defaultOpen={item.isActive} className="group/collapsible">
<SidebarMenuItem>
<CollapsibleTrigger asChild>
<SidebarMenuButton tooltip={item.title}>
{item.icon && <item.icon />}
<span>{item.title}</span>
<ChevronRight className="ml-auto transition-transform duration-200 group-data-[state=open]/collapsible:rotate-90" />
</SidebarMenuButton>
</CollapsibleTrigger>
<CollapsibleContent>
<SidebarMenuSub>
{item.items?.map((subItem) => (
<SidebarMenuSubItem key={subItem.title}>
<SidebarMenuSubButton asChild>
<a href={subItem.url}>
<span>{subItem.title}</span>
</a>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
))}
</SidebarMenuSub>
</CollapsibleContent>
</SidebarMenuItem>
</Collapsible>
))}
</SidebarMenu>
</SidebarGroup>
);
}

View File

@ -0,0 +1,82 @@
"use client";
import { Folder, Forward, MoreHorizontal, Trash2, type LucideIcon } from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
SidebarGroup,
SidebarGroupLabel,
SidebarMenu,
SidebarMenuAction,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from "@/components/ui/sidebar";
export function NavProjects({
projects,
}: {
projects: {
name: string;
url: string;
icon: LucideIcon;
}[];
}) {
const { isMobile } = useSidebar();
return (
<SidebarGroup className="group-data-[collapsible=icon]:hidden">
<SidebarGroupLabel>Projects</SidebarGroupLabel>
<SidebarMenu>
{projects.map((item) => (
<SidebarMenuItem key={item.name}>
<SidebarMenuButton asChild>
<a href={item.url}>
<item.icon />
<span>{item.name}</span>
</a>
</SidebarMenuButton>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuAction showOnHover>
<MoreHorizontal />
<span className="sr-only">More</span>
</SidebarMenuAction>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-48 rounded-lg"
side={isMobile ? "bottom" : "right"}
align={isMobile ? "end" : "start"}>
<DropdownMenuItem>
<Folder className="text-muted-foreground" />
<span>View Project</span>
</DropdownMenuItem>
<DropdownMenuItem>
<Forward className="text-muted-foreground" />
<span>Share Project</span>
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>
<Trash2 className="text-muted-foreground" />
<span>Delete Project</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
))}
<SidebarMenuItem>
<SidebarMenuButton className="text-sidebar-foreground/70">
<MoreHorizontal className="text-sidebar-foreground/70" />
<span>More</span>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
);
}

View File

@ -0,0 +1,100 @@
"use client";
import { BadgeCheck, Bell, ChevronsUpDown, CreditCard, LogOut, Sparkles } from "lucide-react";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar } from "@/components/ui/sidebar";
import { useLogout } from "@/hooks/useLogout";
export function NavUser({
user,
}: {
user: {
name: string;
email: string;
avatar: string;
};
}) {
const { isMobile } = useSidebar();
const logout = useLogout();
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">{user.name}</span>
<span className="truncate text-xs">{user.email}</span>
</div>
<ChevronsUpDown className="ml-auto size-4" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg"
side={isMobile ? "bottom" : "right"}
align="end"
sideOffset={4}>
<DropdownMenuLabel className="p-0 font-normal">
<div className="flex items-center gap-2 px-1 py-1.5 text-left text-sm">
<Avatar className="h-8 w-8 rounded-lg">
<AvatarImage src={user.avatar} alt={user.name} />
<AvatarFallback className="rounded-lg">CN</AvatarFallback>
</Avatar>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">{user.name}</span>
<span className="truncate text-xs">{user.email}</span>
</div>
</div>
</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem>
<Sparkles />
Upgrade to Pro
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuItem>
<BadgeCheck />
Account
</DropdownMenuItem>
<DropdownMenuItem>
<CreditCard />
Billing
</DropdownMenuItem>
<DropdownMenuItem>
<Bell />
Notifications
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={logout}>
<LogOut />
Log out
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
);
}

View File

@ -0,0 +1,74 @@
"use client";
import * as React from "react";
import { ChevronsUpDown, Plus } from "lucide-react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuShortcut,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar } from "@/components/ui/sidebar";
export function TeamSwitcher({
teams,
}: {
teams: {
name: string;
logo: React.ElementType;
plan: string;
}[];
}) {
const { isMobile } = useSidebar();
const [activeTeam, setActiveTeam] = React.useState(teams[0]);
return (
<SidebarMenu>
<SidebarMenuItem>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<SidebarMenuButton
size="lg"
className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground">
<div className="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sidebar-primary-foreground">
<activeTeam.logo className="size-4" />
</div>
<div className="grid flex-1 text-left text-sm leading-tight">
<span className="truncate font-semibold">{activeTeam.name}</span>
<span className="truncate text-xs">{activeTeam.plan}</span>
</div>
<ChevronsUpDown className="ml-auto" />
</SidebarMenuButton>
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-[--radix-dropdown-menu-trigger-width] min-w-56 rounded-lg"
align="start"
side={isMobile ? "bottom" : "right"}
sideOffset={4}>
<DropdownMenuLabel className="text-xs text-muted-foreground">Teams</DropdownMenuLabel>
{teams.map((team, index) => (
<DropdownMenuItem key={team.name} onClick={() => setActiveTeam(team)} className="gap-2 p-2">
<div className="flex size-6 items-center justify-center rounded-sm border">
<team.logo className="size-4 shrink-0" />
</div>
{team.name}
<DropdownMenuShortcut>{index + 1}</DropdownMenuShortcut>
</DropdownMenuItem>
))}
<DropdownMenuSeparator />
<DropdownMenuItem className="gap-2 p-2">
<div className="flex size-6 items-center justify-center rounded-md border bg-background">
<Plus className="size-4" />
</div>
<div className="font-medium text-muted-foreground">Add team</div>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</SidebarMenuItem>
</SidebarMenu>
);
}

View File

@ -0,0 +1,76 @@
import * as React from "react"
import { cn } from "@/lib/utils"
const Card = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
"rounded-xl border bg-card text-card-foreground shadow",
className
)}
{...props}
/>
))
Card.displayName = "Card"
const CardHeader = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex flex-col space-y-1.5 p-6", className)}
{...props}
/>
))
CardHeader.displayName = "CardHeader"
const CardTitle = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("font-semibold leading-none tracking-tight", className)}
{...props}
/>
))
CardTitle.displayName = "CardTitle"
const CardDescription = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
CardDescription.displayName = "CardDescription"
const CardContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("p-6 pt-0", className)} {...props} />
))
CardContent.displayName = "CardContent"
const CardFooter = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex items-center p-6 pt-0", className)}
{...props}
/>
))
CardFooter.displayName = "CardFooter"
export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }

View File

@ -0,0 +1,11 @@
"use client"
import * as CollapsiblePrimitive from "@radix-ui/react-collapsible"
const Collapsible = CollapsiblePrimitive.Root
const CollapsibleTrigger = CollapsiblePrimitive.CollapsibleTrigger
const CollapsibleContent = CollapsiblePrimitive.CollapsibleContent
export { Collapsible, CollapsibleTrigger, CollapsibleContent }

View File

@ -0,0 +1,64 @@
"use client";
import React, { createContext, useContext, useState, useEffect, ReactNode } from "react";
import Cookies from "js-cookie";
interface SessionContextType {
token: string | null;
user: any | null;
setToken: (token: string | null) => void;
setUser: (user: any | null) => void;
loading: boolean;
}
const SessionContext = createContext<SessionContextType | undefined>(undefined);
interface SessionProviderProps {
children: ReactNode;
}
export function SessionProvider({ children }: SessionProviderProps) {
const [token, setTokenState] = useState<string | null>(null);
const [user, setUserState] = useState<any | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const setToken = (newToken: string | null) => {
if (newToken) {
Cookies.set("token", newToken, { expires: 7 });
} else {
Cookies.remove("token");
}
setTokenState(newToken);
};
const setUser = (newUser: any | null) => {
if (newUser) {
localStorage.setItem("user", JSON.stringify(newUser));
} else {
localStorage.removeItem("user");
}
setUserState(newUser);
};
useEffect(() => {
const storedToken = Cookies.get("token") || null;
const storedUser = localStorage.getItem("user");
if (storedToken) {
setTokenState(storedToken);
}
if (storedUser) {
try {
setUserState(JSON.parse(storedUser));
} catch (error) {
console.error("Failed to parse stored user.", error);
}
}
setLoading(false);
}, []);
return (
<SessionContext.Provider value={{ token, user, setToken, setUser, loading }}>{children}</SessionContext.Provider>
);
}
export { SessionContext };

View File

@ -0,0 +1,30 @@
"use client";
import { useContext } from "react";
import { useRouter } from "next/navigation";
import { SessionContext } from "@/context/SessionContext";
import Cookies from "js-cookie";
export function useLogout() {
const router = useRouter();
const context = useContext(SessionContext);
if (!context) {
throw new Error("useLogout must be used within a SessionProvider");
}
const { setToken, setUser } = context;
const logout = () => {
Cookies.remove("token");
Cookies.remove("user");
setToken(null);
setUser(null);
console.log(Cookies.get("token"));
router.push("/");
};
return logout;
}

View File

@ -0,0 +1,18 @@
"use client";
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useSession } from "./useSession";
export function useProtectedRoute() {
const { data: session } = useSession();
const router = useRouter();
useEffect(() => {
if (!session?.token) {
router.push("/signin");
}
}, [session?.token, router]);
return session?.token;
}

View File

@ -0,0 +1,22 @@
"use client";
import { useContext } from "react";
import { SessionContext } from "@/context/SessionContext";
export function useSession() {
const context = useContext(SessionContext);
if (!context) {
throw new Error("useSession must be used within a SessionProvider");
}
const { token, user, loading } = context;
let status: "loading" | "authenticated" | "unauthenticated";
if (loading) status = "loading";
else if (token) status = "authenticated";
else status = "unauthenticated";
const session = token ? { token, user } : null;
return { data: session, status };
}

View File

@ -12,6 +12,7 @@
"@hookform/resolvers": "^4.0.0",
"@radix-ui/react-avatar": "^1.1.3",
"@radix-ui/react-checkbox": "^1.1.4",
"@radix-ui/react-collapsible": "^1.1.3",
"@radix-ui/react-dialog": "^1.1.6",
"@radix-ui/react-dropdown-menu": "^2.1.6",
"@radix-ui/react-label": "^2.1.2",
@ -23,10 +24,13 @@
"@react-google-maps/api": "^2.20.6",
"@tailwindcss/typography": "^0.5.16",
"@tanstack/react-query": "^5.66.0",
"axios": "^1.7.9",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"js-cookie": "^3.0.5",
"lucide-react": "^0.475.0",
"next": "15.1.0",
"next-auth": "^4.24.11",
"next-themes": "^0.4.4",
"react": "^19.0.0",
"react-dom": "^19.0.0",

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
import { z } from "zod";
export const signInSchema = z.object({
email: z
.string({ required_error: "Email is required" })
.min(1, { message: "Email is required" })
.email({ message: "Invalid email address" }),
password: z
.string({ required_error: "Password is required" })
.min(6, { message: "Password must be at least 6 characters long" }),
});
export const signUpSchema = z
.object({
email: z
.string({ required_error: "Email is required" })
.min(1, { message: "Email is required" })
.email({ message: "Invalid email address" }),
password: z
.string({ required_error: "Password is required" })
.min(6, { message: "Password must be at least 6 characters" }),
confirmPassword: z
.string({ required_error: "Confirm your password" })
.min(6, { message: "Confirm Password must be at least 6 characters" }),
})
.refine((data) => data.password === data.confirmPassword, {
message: "Passwords do not match",
path: ["confirmPassword"],
});

View File

@ -0,0 +1,15 @@
import * as z from "zod";
export const farmFormSchema = z.object({
name: z.string().min(2, "Farm name must be at least 2 characters"),
location: z.string().min(2, "Location must be at least 2 characters"),
type: z.string().min(1, "Please select a farm type"),
});
export const cropFormSchema = z.object({
name: z.string().min(2, "Crop name must be at least 2 characters"),
plantedDate: z.string().refine((val) => !Number.isNaN(Date.parse(val)), {
message: "Please enter a valid date",
}),
status: z.enum(["growing", "harvested", "planned"]),
});

15
frontend/types.ts Normal file
View File

@ -0,0 +1,15 @@
export interface Crop {
id: string;
farmId: string;
name: string;
plantedDate: Date;
status: "growing" | "harvested" | "planned";
}
export interface Farm {
id: string;
name: string;
location: string;
type: string;
createdAt: Date;
}

5
package.json Normal file
View File

@ -0,0 +1,5 @@
{
"dependencies": {
"@types/js-cookie": "^3.0.6"
}
}

22
pnpm-lock.yaml Normal file
View File

@ -0,0 +1,22 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.:
dependencies:
'@types/js-cookie':
specifier: ^3.0.6
version: 3.0.6
packages:
'@types/js-cookie@3.0.6':
resolution: {integrity: sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==}
snapshots:
'@types/js-cookie@3.0.6': {}