-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implemented post request * fix: added location to Event struct * fix: follows convention of struct field tags * fix: added apimodels package * fix: edited Create() * fix: parse string to time object * fix: naming convention * chore: rename token * fix: overwritten changes to .gitignore --------- Co-authored-by: Rustam Nassyrov <[email protected]>
- Loading branch information
1 parent
bb0f1ed
commit c450524
Showing
7 changed files
with
159 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package apimodels | ||
|
||
type CreateEvent struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
StartDate string `json:"startDate"` | ||
EndDate string `json:"endDate"` | ||
VolunteersRequired int `json:"volunteersRequired"` | ||
Location string `json:"location"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"blood-for-life-backend/apimodels" | ||
"blood-for-life-backend/store" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/joho/godotenv" | ||
"github.com/labstack/echo" | ||
_ "github.com/lib/pq" | ||
) | ||
|
||
func main() { | ||
e := echo.New() | ||
|
||
envErr := godotenv.Load(".env") | ||
|
||
if envErr != nil { | ||
fmt.Println(envErr.Error()) | ||
} | ||
|
||
dbConnection := os.Getenv("DB") | ||
|
||
db, err := sqlx.Connect("postgres", dbConnection) | ||
|
||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
defer db.Close() | ||
|
||
if err := db.Ping(); err != nil { | ||
fmt.Println(err.Error()) | ||
} else { | ||
fmt.Println("Successfully Connected") | ||
} | ||
|
||
loadSchema(db) | ||
|
||
eventStore := store.NewPGEventStore(db) | ||
bind(e, eventStore) | ||
|
||
e.Logger.Fatal(e.Start(":1323")) | ||
|
||
} | ||
|
||
func loadSchema(db *sqlx.DB) { | ||
file, err := os.ReadFile("./db/schema.sql") | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
_, err = db.Exec(string(file)) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
} | ||
|
||
func bind(e *echo.Echo, eventStore store.EventStore) { | ||
e.GET("/", func(c echo.Context) error { | ||
return c.String(http.StatusOK, "Hello, World!") | ||
}) | ||
e.Logger.Fatal(e.Start(":1323")) | ||
|
||
e.POST("/api/create", func(c echo.Context) error { | ||
event := new(apimodels.CreateEvent) | ||
|
||
// parse request body | ||
bindErr := c.Bind(event) | ||
if bindErr != nil { | ||
return c.JSON(http.StatusBadRequest, bindErr) | ||
} | ||
|
||
// convert string to time.Time | ||
start, _ := time.Parse("01/02/2006 03:04 PM", event.StartDate) | ||
end, _ := time.Parse("01/02/2006 03:04 PM", event.EndDate) | ||
|
||
_, err := eventStore.Create(c.Request().Context(), event.Name, event.Description, start, end, event.VolunteersRequired, event.Location) | ||
|
||
if err != nil { | ||
return c.JSON(http.StatusBadRequest, err) | ||
} | ||
|
||
return c.JSON(http.StatusOK, event) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters