Project Add, Update, Get, Get many added
This commit is contained in:
parent
c4fc1fc9b0
commit
3f73542c64
@ -15,11 +15,17 @@ func ApiRoutes() *http.ServeMux {
|
|||||||
mux.HandleFunc("GET /check", handlers.CheckRoleHandler)
|
mux.HandleFunc("GET /check", handlers.CheckRoleHandler)
|
||||||
|
|
||||||
//user
|
//user
|
||||||
mux.HandleFunc("GET /user/{id}", handlers.GetUser)
|
mux.HandleFunc("GET /user/{id}", handlers.GetUserHandler)
|
||||||
|
|
||||||
//auth
|
//auth
|
||||||
mux.HandleFunc("POST /login", handlers.Login)
|
mux.HandleFunc("POST /login", handlers.Login)
|
||||||
mux.HandleFunc("POST /register", handlers.CreateUser)
|
mux.HandleFunc("POST /register", handlers.CreateUserHandler)
|
||||||
|
|
||||||
|
//Project
|
||||||
|
mux.HandleFunc("POST /project", handlers.CreateProjectHandler)
|
||||||
|
mux.HandleFunc("PATCH /project/{id}", handlers.UpdateProjectHandler)
|
||||||
|
mux.HandleFunc("GET /project/{id}", handlers.GetProjectHandler)
|
||||||
|
mux.HandleFunc("GET /projects", handlers.GetProjectsHandler)
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,3 +44,12 @@ func UnauthorizedHandler(w http.ResponseWriter) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NotImplementedHandler(w http.ResponseWriter) {
|
||||||
|
w.WriteHeader(http.StatusNotImplemented) //set http status
|
||||||
|
_, err := w.Write([]byte("WORK IN PROGRESS"))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@ -1 +1,105 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"portfolio/api/service/jwt"
|
||||||
|
"portfolio/database/ent"
|
||||||
|
"portfolio/database/query"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
id, _, err := jwt.VerifyUser(r)
|
||||||
|
if err != nil {
|
||||||
|
UnauthorizedHandler(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var p *ent.Project
|
||||||
|
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&p)
|
||||||
|
if err != nil {
|
||||||
|
InternalServerErrorHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = query.CreateProject(context.Background(), *p, id)
|
||||||
|
if err != nil {
|
||||||
|
UnprocessableEntityHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateProjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
_, _, err := jwt.VerifyUser(r)
|
||||||
|
if err != nil {
|
||||||
|
UnauthorizedHandler(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var p *ent.Project
|
||||||
|
|
||||||
|
projectID, err := strconv.Atoi(r.PathValue("id"))
|
||||||
|
if err != nil {
|
||||||
|
BadRequestHandler(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err = query.GetFullProject(context.Background(), projectID)
|
||||||
|
if err != nil {
|
||||||
|
UnprocessableEntityHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&p)
|
||||||
|
if err != nil {
|
||||||
|
InternalServerErrorHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = query.UpdateProject(context.Background(), *p, projectID)
|
||||||
|
if err != nil {
|
||||||
|
UnprocessableEntityHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetProjectHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
projectID, err := strconv.Atoi(r.PathValue("id"))
|
||||||
|
if err != nil {
|
||||||
|
BadRequestHandler(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := query.GetProject(context.Background(), projectID)
|
||||||
|
if err != nil {
|
||||||
|
UnprocessableEntityHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
err = json.NewEncoder(w).Encode(p)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetProjectsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
p, err := query.GetProjects(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
UnprocessableEntityHandler(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
err = json.NewEncoder(w).Encode(p)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateUser(w http.ResponseWriter, r *http.Request) {
|
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var u *ent.User
|
var u *ent.User
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUser(w http.ResponseWriter, r *http.Request) {
|
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
userID, err := strconv.Atoi(r.PathValue("id"))
|
userID, err := strconv.Atoi(r.PathValue("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -2,11 +2,24 @@ package jwt
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_ "context"
|
_ "context"
|
||||||
|
"fmt"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func VerifyUser(r *http.Request) (int, string, error) {
|
||||||
|
bearerToken := r.Header.Get("Authorization")
|
||||||
|
jwtToken := ""
|
||||||
|
if len(bearerToken) > 7 {
|
||||||
|
jwtToken = bearerToken[len("Bearer "):]
|
||||||
|
} else {
|
||||||
|
return 0, "", fmt.Errorf("token empty")
|
||||||
|
}
|
||||||
|
return VerifyJWT(jwtToken)
|
||||||
|
}
|
||||||
|
|
||||||
// VerifyJWT verify JWT token and returns user object
|
// VerifyJWT verify JWT token and returns user object
|
||||||
func VerifyJWT(authToken string) (int, string, error) {
|
func VerifyJWT(authToken string) (int, string, error) {
|
||||||
//get jwt secret from environment
|
//get jwt secret from environment
|
||||||
|
|||||||
@ -26,7 +26,7 @@ var (
|
|||||||
// TeamsColumns holds the columns for the "teams" table.
|
// TeamsColumns holds the columns for the "teams" table.
|
||||||
TeamsColumns = []*schema.Column{
|
TeamsColumns = []*schema.Column{
|
||||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||||
{Name: "name", Type: field.TypeString},
|
{Name: "name", Type: field.TypeString, Unique: true},
|
||||||
}
|
}
|
||||||
// TeamsTable holds the schema information for the "teams" table.
|
// TeamsTable holds the schema information for the "teams" table.
|
||||||
TeamsTable = &schema.Table{
|
TeamsTable = &schema.Table{
|
||||||
@ -38,9 +38,9 @@ var (
|
|||||||
UsersColumns = []*schema.Column{
|
UsersColumns = []*schema.Column{
|
||||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||||
{Name: "name", Type: field.TypeString, Unique: true},
|
{Name: "name", Type: field.TypeString, Unique: true},
|
||||||
{Name: "email", Type: field.TypeString},
|
{Name: "email", Type: field.TypeString, Unique: true},
|
||||||
{Name: "password", Type: field.TypeString},
|
{Name: "password", Type: field.TypeString},
|
||||||
{Name: "role", Type: field.TypeEnum, Enums: []string{"admin", "user", "visitor"}},
|
{Name: "role", Type: field.TypeEnum, Enums: []string{"owner", "admin", "user", "visitor"}},
|
||||||
}
|
}
|
||||||
// UsersTable holds the schema information for the "users" table.
|
// UsersTable holds the schema information for the "users" table.
|
||||||
UsersTable = &schema.Table{
|
UsersTable = &schema.Table{
|
||||||
|
|||||||
@ -73,6 +73,7 @@ type Role string
|
|||||||
|
|
||||||
// Role values.
|
// Role values.
|
||||||
const (
|
const (
|
||||||
|
RoleOwner Role = "owner"
|
||||||
RoleAdmin Role = "admin"
|
RoleAdmin Role = "admin"
|
||||||
RoleUser Role = "user"
|
RoleUser Role = "user"
|
||||||
RoleVisitor Role = "visitor"
|
RoleVisitor Role = "visitor"
|
||||||
@ -85,7 +86,7 @@ func (r Role) String() string {
|
|||||||
// RoleValidator is a validator for the "role" field enum values. It is called by the builders before save.
|
// RoleValidator is a validator for the "role" field enum values. It is called by the builders before save.
|
||||||
func RoleValidator(r Role) error {
|
func RoleValidator(r Role) error {
|
||||||
switch r {
|
switch r {
|
||||||
case RoleAdmin, RoleUser, RoleVisitor:
|
case RoleOwner, RoleAdmin, RoleUser, RoleVisitor:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("user: invalid enum value for role field: %q", r)
|
return fmt.Errorf("user: invalid enum value for role field: %q", r)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user