diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 2b3cced..5a88873 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -12,7 +12,9 @@ import ( // Interface for the database type Database interface { + // Insert a new user into the database, password should be hashed before calling AddUser(username string, password string) error + RemoveUser(username string) error PromoteToAdmin(username string) error GetUserId(username string) (int, error) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 689759b..9c42133 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -11,11 +11,11 @@ import ( // The actual interface that we will use type GlobalState interface { - Register(c *fiber.Ctx) error // To register a new user - UserDelete(c *fiber.Ctx) error // To delete a user - Login(c *fiber.Ctx) error // To get the token - LoginRenew(c *fiber.Ctx) error // To renew the token - // CreateProject(c *fiber.Ctx) error // To create a new project + Register(c *fiber.Ctx) error // To register a new user + UserDelete(c *fiber.Ctx) error // To delete a user + Login(c *fiber.Ctx) error // To get the token + LoginRenew(c *fiber.Ctx) error // To renew the token + CreateProject(c *fiber.Ctx) error // To create a new project // GetProjects(c *fiber.Ctx) error // To get all projects // GetProject(c *fiber.Ctx) error // To get a specific project // UpdateProject(c *fiber.Ctx) error // To update a project @@ -58,7 +58,7 @@ type GState struct { // @Failure 500 {string} string "Internal server error" // @Router /api/register [post] func (gs *GState) Register(c *fiber.Ctx) error { - u := new(types.User) + u := new(types.NewUser) if err := c.BodyParser(u); err != nil { return c.Status(400).SendString(err.Error()) } @@ -142,3 +142,24 @@ func (gs *GState) LoginRenew(c *fiber.Ctx) error { } return c.JSON(fiber.Map{"token": t}) } + +// CreateProject is a simple handler that creates a new project +func (gs *GState) CreateProject(c *fiber.Ctx) error { + user := c.Locals("user").(*jwt.Token) + + p := new(types.NewProject) + if err := c.BodyParser(p); err != nil { + return c.Status(400).SendString(err.Error()) + } + + // Get the username from the token and set it as the owner of the project + // This is ugly but + claims := user.Claims.(jwt.MapClaims) + p.Owner = claims["name"].(string) + + if err := gs.Db.AddProject(p.Name, p.Description, p.Owner); err != nil { + return c.Status(500).SendString(err.Error()) + } + + return c.Status(200).SendString("Project added") +} diff --git a/backend/internal/types/project.go b/backend/internal/types/project.go new file mode 100644 index 0000000..cabf6c6 --- /dev/null +++ b/backend/internal/types/project.go @@ -0,0 +1,21 @@ +package types + +import ( + "time" +) + +// Project is a struct that holds the information about a project +type Project struct { + ID int `json:"id" db:"id"` + Name string `json:"name" db:"name"` + Description string `json:"description" db:"description"` + Owner string `json:"owner" db:"owner"` + Created time.Time `json:"created" db:"created"` +} + +// As it arrives from the client +type NewProject struct { + Name string `json:"name"` + Description string `json:"description"` + Owner string `json:"owner"` +} diff --git a/backend/internal/types/users.go b/backend/internal/types/users.go index fa735d7..233ec71 100644 --- a/backend/internal/types/users.go +++ b/backend/internal/types/users.go @@ -16,6 +16,11 @@ func (u *User) ToPublicUser() (*PublicUser, error) { }, nil } +type NewUser struct { + Username string `json:"username"` + Password string `json:"password"` +} + // PublicUser represents a user that is safe to send over the API (no password) type PublicUser struct { UserId string `json:"userId"`