resolving conflicts
This commit is contained in:
@@ -2,21 +2,32 @@ package blogs
|
||||
|
||||
import (
|
||||
"context"
|
||||
<<<<<<< HEAD
|
||||
"fmt"
|
||||
=======
|
||||
>>>>>>> dev/cedric
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
<<<<<<< HEAD
|
||||
func HandleGetBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
var blog []models.Blog
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&blog).
|
||||
=======
|
||||
func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
blog_uuid := r.PathValue("uuid")
|
||||
|
||||
var blog core.Blog
|
||||
var blog models.Blog
|
||||
_, err := core.DB.NewSelect().
|
||||
Model(&blog).
|
||||
Where("blog_id = ?", blog_uuid).
|
||||
>>>>>>> dev/cedric
|
||||
Relation("Author").
|
||||
ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
@@ -29,11 +40,17 @@ func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
<<<<<<< HEAD
|
||||
Message: fmt.Sprintf("%d blogs objects sent", count),
|
||||
=======
|
||||
Message: "Status OK",
|
||||
>>>>>>> dev/cedric
|
||||
Data: blog,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
<<<<<<<< HEAD:backend/api/blogs/blog.go
|
||||
|
||||
func HandleBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
var blog []core.Blog
|
||||
@@ -56,3 +73,7 @@ func HandleBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
========
|
||||
>>>>>>>> dev/cedric:backend/api/blogs/blogs.go
|
||||
=======
|
||||
>>>>>>> dev/cedric
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
package blogs
|
||||
<<<<<<< HEAD
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -30,3 +31,5 @@ func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
Message: "Blog deleted.",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
=======
|
||||
>>>>>>> dev/cedric
|
||||
|
||||
@@ -3,9 +3,11 @@ package blogs
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"io"
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -17,7 +19,7 @@ func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var blog core.Blog
|
||||
var blog models.Blog
|
||||
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
|
||||
15
backend/api/blogs_routes.go
Normal file
15
backend/api/blogs_routes.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/blogs"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var BlogsRoutes = map[string]core.Handler{
|
||||
"/blogs/new": {
|
||||
Handler: blogs.HandleNew,
|
||||
Middlewares: []core.Middleware{Methods(("POST")), AuthJWT}},
|
||||
"/blogs/{uuid}": {
|
||||
Handler: blogs.HandleBlog,
|
||||
Middlewares: []core.Middleware{Methods("GET")}},
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package core
|
||||
|
||||
var CSRF_KEY = []byte("32-byte-long-auth-key")
|
||||
@@ -1,8 +0,0 @@
|
||||
package core
|
||||
|
||||
type Paginated[T any] struct {
|
||||
Page int `json:"page"`
|
||||
Limit int `json:"limit"`
|
||||
TotalPages int `json:"totalPages"`
|
||||
Items []T `json:"items"`
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type JSONStatus string
|
||||
|
||||
const (
|
||||
Error JSONStatus = "Error"
|
||||
Success JSONStatus = "Success"
|
||||
)
|
||||
|
||||
type JSONResponse interface {
|
||||
ToJSON() ([]byte, error)
|
||||
Respond(w http.ResponseWriter, code int)
|
||||
}
|
||||
|
||||
type JSONError struct {
|
||||
Status JSONStatus `json:"status"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type JSONSuccess struct {
|
||||
Status JSONStatus `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (r *JSONError) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(r)
|
||||
}
|
||||
|
||||
func (r *JSONSuccess) ToJSON() ([]byte, error) {
|
||||
return json.Marshal(r)
|
||||
}
|
||||
|
||||
func defaultResponse(r JSONResponse, w http.ResponseWriter, code int) {
|
||||
jsonData, err := r.ToJSON()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotAcceptable)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
||||
func (r JSONError) Respond(w http.ResponseWriter, code int) {
|
||||
defaultResponse(&r, w, code)
|
||||
}
|
||||
|
||||
func (r JSONSuccess) Respond(w http.ResponseWriter, code int) {
|
||||
defaultResponse(&r, w, code)
|
||||
}
|
||||
|
||||
type Middleware func(http.Handler) http.Handler
|
||||
|
||||
type Handler struct {
|
||||
Handler http.HandlerFunc
|
||||
Middlewares []Middleware
|
||||
}
|
||||
|
||||
func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) {
|
||||
for pattern, handler := range routes {
|
||||
if handler.Middlewares == nil {
|
||||
mux.HandleFunc(pattern, handler.Handler)
|
||||
} else {
|
||||
h := http.HandlerFunc(handler.Handler)
|
||||
for _, middleware := range handler.Middlewares {
|
||||
h = http.HandlerFunc(middleware(h).ServeHTTP)
|
||||
}
|
||||
mux.Handle(pattern, h)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
)
|
||||
|
||||
var DB *bun.DB
|
||||
|
||||
type DSN struct {
|
||||
Hostname string
|
||||
Port string
|
||||
DBName string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (dsn *DSN) ToString() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName)
|
||||
}
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
Active Status = "Active"
|
||||
Inactive Status = "Inactive"
|
||||
)
|
||||
|
||||
type Group string
|
||||
|
||||
const (
|
||||
LatosaGroup Group = "latosa"
|
||||
WingTsunGroup Group = "wing-tsun"
|
||||
)
|
||||
|
||||
type UserAttributes struct {
|
||||
Groups []Group `json:"groups"`
|
||||
}
|
||||
|
||||
type PermissionConditions struct {
|
||||
Groups *[]Group `json:"groups,omitempty"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
bun.BaseModel `bun:"table:users"`
|
||||
|
||||
UserID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"userId"`
|
||||
FirstName string `bun:"firstname,notnull" json:"firstname"`
|
||||
LastName string `bun:"lastname,notnull" json:"lastname"`
|
||||
Email string `bun:"email,unique,notnull" json:"email"`
|
||||
Password string `bun:"password,notnull" json:"password,omitempty"`
|
||||
Phone string `bun:"phone,notnull" json:"phone"`
|
||||
CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"`
|
||||
UpdatedAt time.Time `bun:"updated_at,default:current_timestamp" json:"updatedAt"`
|
||||
Events []Event `bun:"m2m:events_to_users,join:User=Event" json:"events,omitempty"`
|
||||
Articles []*Blog `bun:"rel:has-many,join:user_id=blog_id" json:"articles,omitempty"`
|
||||
Attributes UserAttributes `bun:"attributes,type:jsonb" json:"attributes"`
|
||||
}
|
||||
|
||||
func (u *User) Insert(ctx context.Context) (sql.Result, error) {
|
||||
u.Password = fmt.Sprintf("crypt('%s', gen_salt('bf'))", u.Password)
|
||||
return DB.NewInsert().
|
||||
Model(u).
|
||||
Value("password", u.Password).
|
||||
Exec(ctx)
|
||||
}
|
||||
|
||||
func Verify(ctx context.Context, email, password string) (*User, error) {
|
||||
var user User
|
||||
count, err := DB.NewSelect().
|
||||
Model(&user).
|
||||
Where("email = ? AND password = crypt(?, password)", email, password).
|
||||
Limit(1).
|
||||
ScanAndCount(context.Background())
|
||||
if count == 0 {
|
||||
return nil, fmt.Errorf("invalid email or password")
|
||||
}
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("invalid email or password")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
type Permission struct {
|
||||
bun.BaseModel `bun:"table:permissions"`
|
||||
ID int `bun:"id,pk,autoincrement" json:"id"`
|
||||
Resource string `bun:"resource,notnull" json:"resource"`
|
||||
Action string `bun:"action,notnull" json:"action"`
|
||||
Conditions PermissionConditions `bun:"conditions,type:jsonb" json:"conditions"`
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
bun.BaseModel `bun:"table:roles"`
|
||||
ID uuid.UUID `bun:"id,pk,type:uuid,default:gen_random_uuid()" json:"id"`
|
||||
Name string `bun:"name,unique,notnull" json:"name"`
|
||||
}
|
||||
|
||||
type PermissionToRole struct {
|
||||
bun.BaseModel `bun:"table:permissions_to_users"`
|
||||
|
||||
PermissionID int `bun:"permission_id,pk"`
|
||||
RoleID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
Permission *Permission `bun:"rel:belongs-to,join:permission_id=id"`
|
||||
Role *Role `bun:"rel:belongs-to,join:role_id=id"`
|
||||
}
|
||||
|
||||
type UserToRole struct {
|
||||
bun.BaseModel `bun:"table:users_to_roles"`
|
||||
|
||||
UserID uuid.UUID `bun:"user_id,type:uuid,pk"`
|
||||
RoleID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
User *User `bun:"rel:belongs-to,join:user_id=user_id"`
|
||||
Role *Role `bun:"rel:belongs-to,join:role_id=id"`
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
bun.BaseModel `bun:"table:events"`
|
||||
|
||||
EventID uuid.UUID `bun:"event_id,type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
CreationDate time.Time `bun:"creation_date,notnull,default:current_timestamp" json:"creationDate"`
|
||||
ScheduleStart time.Time `bun:"schedule_start,notnull" json:"start"`
|
||||
ScheduleEnd time.Time `bun:"schedule_end,notnull" json:"end"`
|
||||
Status Status `bun:"status,notnull,default:'Inactive'" json:"status"`
|
||||
}
|
||||
|
||||
type EventToUser struct {
|
||||
bun.BaseModel `bun:"table:events_to_users"`
|
||||
|
||||
EventID uuid.UUID `bun:"type:uuid,pk"`
|
||||
UserID uuid.UUID `bun:"type:uuid,pk"`
|
||||
|
||||
Event *Event `bun:"rel:belongs-to,join:event_id=event_id"`
|
||||
User *User `bun:"rel:belongs-to,join:user_id=user_id"`
|
||||
}
|
||||
|
||||
type Blog struct {
|
||||
bun.BaseModel `bun:"table:blogs"`
|
||||
|
||||
BlogID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"blogID"`
|
||||
Slug string `bun:"slug,unique,notnull" json:"slug"`
|
||||
Content string `bun:"content,notnull" json:"content"`
|
||||
Label string `bun:"label" json:"label"`
|
||||
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||
Published time.Time `bun:"published,default:current_timestamp" json:"published"`
|
||||
Summary string `bun:"summary" json:"summary"`
|
||||
Image string `bun:"image" json:"image"`
|
||||
Href string `bun:"href" json:"href"`
|
||||
|
||||
Author User `bun:"rel:belongs-to,join:author_id=user_id" json:"author"`
|
||||
}
|
||||
|
||||
type WebsiteSettings struct {
|
||||
bun.BaseModel `bun:"table:website_settings"`
|
||||
|
||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
AutoAcceptDemand bool `bun:"auto_accept_demand,default:false" json:"autoAcceptDemand"`
|
||||
}
|
||||
|
||||
type Media struct {
|
||||
bun.BaseModel `bun:"table:media"`
|
||||
ID uuid.UUID `bun:"type:uuid,pk,default:gen_random_uuid()" json:"id"`
|
||||
AuthorID uuid.UUID `bun:"author_id,type:uuid,notnull" json:"authorID"`
|
||||
Author *User `bun:"rel:belongs-to,join:author_id=user_id" json:"author,omitempty"`
|
||||
Type string `bun:"media_type" json:"type"` // Image, Video, GIF etc. Add support for PDFs?
|
||||
Alt string `bun:"media_alt" json:"alt"`
|
||||
Path string `bun:"media_path" json:"path"`
|
||||
Size int64 `bun:"media_size" json:"size"`
|
||||
URL string `bun:"-" json:"url"`
|
||||
}
|
||||
|
||||
type ShortcodeType string
|
||||
|
||||
const (
|
||||
ShortcodeMedia ShortcodeType = "media"
|
||||
ShortcodeValue ShortcodeType = "value"
|
||||
)
|
||||
|
||||
type Shortcode struct {
|
||||
bun.BaseModel `bun:"table:shortcodes,alias:sc"`
|
||||
|
||||
ID int64 `bun:"id,pk,autoincrement" json:"id"` // Primary key
|
||||
Code string `bun:"code,notnull,unique" json:"code"` // The shortcode value
|
||||
Type ShortcodeType `bun:"shortcode_type,notnull" json:"type"`
|
||||
Value *string `bun:"value" json:"value,omitempty"`
|
||||
MediaID *uuid.UUID `bun:"media_id,type:uuid" json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
Media *Media `bun:"rel:belongs-to,join:media_id=id" json:"media,omitempty"` // Relation to Media
|
||||
}
|
||||
|
||||
func (s *Shortcode) Validate() error {
|
||||
if s.Value != nil && s.MediaID != nil {
|
||||
return errors.New("both value and media_id cannot be set at the same time")
|
||||
}
|
||||
if s.Value == nil && s.MediaID == nil {
|
||||
return errors.New("either value or media_id must be set")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitDatabase(dsn DSN) (*bun.DB, error) {
|
||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn.ToString())))
|
||||
db := bun.NewDB(sqldb, pgdialect.New())
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := db.ExecContext(ctx, "CREATE EXTENSION IF NOT EXISTS pgcrypto;")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.RegisterModel((*EventToUser)(nil))
|
||||
db.RegisterModel((*PermissionToRole)(nil))
|
||||
db.RegisterModel((*UserToRole)(nil))
|
||||
_, err = db.NewCreateTable().Model((*User)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Event)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*EventToUser)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Blog)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Media)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Shortcode)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Role)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*Permission)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*PermissionToRole)(nil)).IfNotExists().Exec(ctx)
|
||||
_, err = db.NewCreateTable().Model((*UserToRole)(nil)).IfNotExists().Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
@@ -5,12 +5,13 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("event_uuid")
|
||||
var event core.Event
|
||||
var event models.Event
|
||||
res, err := core.DB.NewDelete().
|
||||
Model(&event).
|
||||
Where("event_id = ?", uuid).
|
||||
|
||||
@@ -2,15 +2,15 @@ package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleEvent(w http.ResponseWriter, r *http.Request) {
|
||||
event_uuid := r.PathValue("event_uuid")
|
||||
var event core.Event
|
||||
var event models.Event
|
||||
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
@@ -27,22 +27,3 @@ func HandleEvent(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
func HandleEvents(w http.ResponseWriter, r *http.Request) {
|
||||
var events []core.Event
|
||||
rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: fmt.Sprintf("%d Event successfully sent", rowsCount),
|
||||
Data: events,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
29
backend/api/events/events.go
Normal file
29
backend/api/events/events.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleEvents(w http.ResponseWriter, r *http.Request) {
|
||||
var events []models.Event
|
||||
rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: fmt.Sprintf("%d Event successfully sent", rowsCount),
|
||||
Data: events,
|
||||
}.Respond(w, http.StatusOK)
|
||||
return
|
||||
}
|
||||
@@ -5,11 +5,12 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var event core.Event
|
||||
var event models.Event
|
||||
err := json.NewDecoder(r.Body).Decode(&event)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
|
||||
@@ -6,12 +6,13 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var event core.Event
|
||||
var event models.Event
|
||||
err := json.NewDecoder(r.Body).Decode(&event)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
@@ -32,6 +33,7 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
log.Println(event)
|
||||
|
||||
_, err = core.DB.NewUpdate().
|
||||
Model(&event).
|
||||
OmitZero().
|
||||
|
||||
24
backend/api/events_routes.go
Normal file
24
backend/api/events_routes.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/events"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var EventsRoutes = map[string]core.Handler{
|
||||
"/events": {
|
||||
Handler: events.HandleEvents,
|
||||
Middlewares: []core.Middleware{Methods("GET")}},
|
||||
"/events/new": {
|
||||
Handler: events.HandleNew,
|
||||
Middlewares: []core.Middleware{Methods("POST"), AuthJWT}},
|
||||
"/events/{event_uuid}": {
|
||||
Handler: events.HandleEvent,
|
||||
Middlewares: []core.Middleware{Methods("GET")}},
|
||||
"/events/{event_uuid}/delete": {
|
||||
Handler: events.HandleDelete,
|
||||
Middlewares: []core.Middleware{Methods("DELETE"), AuthJWT}},
|
||||
"/events/{event_uuid}/update": {
|
||||
Handler: events.HandleUpdate,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT}},
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"github.com/gorilla/csrf"
|
||||
)
|
||||
|
||||
|
||||
@@ -6,12 +6,13 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
var media models.Media
|
||||
res, err := core.DB.NewDelete().
|
||||
Model(&media).
|
||||
Where("id = ?", uuid).
|
||||
|
||||
@@ -7,7 +7,8 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
@@ -32,12 +33,12 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
offset := (page - 1) * limit
|
||||
|
||||
total, err := core.DB.NewSelect().
|
||||
Model((*core.Media)(nil)).
|
||||
Model((*models.Media)(nil)).
|
||||
Count(context.Background())
|
||||
|
||||
totalPages := int(math.Max(1, float64(total/limit)))
|
||||
|
||||
var media []core.Media
|
||||
var media []models.Media
|
||||
err = core.DB.NewSelect().
|
||||
Model(&media).
|
||||
Limit(limit).
|
||||
@@ -51,7 +52,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
baseURL := utils.GetURL(r)
|
||||
media = utils.Map(media, func(m core.Media) core.Media {
|
||||
media = utils.Map(media, func(m models.Media) models.Media {
|
||||
m.Author = nil
|
||||
m.URL = fmt.Sprintf("%s%s/file", baseURL, m.ID)
|
||||
return m
|
||||
@@ -60,7 +61,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Media successfully retrieved",
|
||||
Data: core.Paginated[core.Media]{
|
||||
Data: core.Paginated[models.Media]{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
TotalPages: totalPages,
|
||||
@@ -71,7 +72,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
var media models.Media
|
||||
err := core.DB.NewSelect().
|
||||
Model(&media).
|
||||
Where("id = ?", uuid).
|
||||
@@ -98,7 +99,7 @@ func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func HandleMediaFile(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("media_uuid")
|
||||
var media core.Media
|
||||
var media models.Media
|
||||
err := core.DB.NewSelect().
|
||||
Model(&media).
|
||||
Where("id = ?", uuid).
|
||||
|
||||
@@ -8,7 +8,8 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
@@ -95,7 +96,7 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
media := &core.Media{
|
||||
media := &models.Media{
|
||||
AuthorID: id,
|
||||
Type: fileHeader.Header.Get("Content-Type"),
|
||||
Alt: "To be implemented",
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
|
||||
39
backend/api/media_routes.go
Normal file
39
backend/api/media_routes.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/media"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var MediaRoutes = map[string]core.Handler{
|
||||
"/media/upload": {
|
||||
Handler: media.HandleUpload,
|
||||
Middlewares: []core.Middleware{Methods("POST"), AuthJWT}},
|
||||
"/media/verify": {
|
||||
Handler: media.HandleVerify,
|
||||
Middlewares: []core.Middleware{Methods("POST"), AuthJWT},
|
||||
},
|
||||
// Paginated media response
|
||||
"/media/": {
|
||||
Handler: media.HandleMedia,
|
||||
Middlewares: []core.Middleware{Methods("GET")},
|
||||
},
|
||||
// Unique element
|
||||
"/media/{media_uuid}": {
|
||||
Handler: media.HandleMediaDetails,
|
||||
Middlewares: []core.Middleware{Methods("GET")},
|
||||
},
|
||||
// Get the image, video, GIF etc.
|
||||
"/media/{media_uuid}/file": {
|
||||
Handler: media.HandleMediaFile,
|
||||
Middlewares: []core.Middleware{Methods("GET")},
|
||||
},
|
||||
// "/media/{media_uuid}/update": {
|
||||
// Handler: HandleGetMediaFile,
|
||||
// Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
// },
|
||||
"/media/{media_uuid}/delete": {
|
||||
Handler: media.HandleDelete,
|
||||
Middlewares: []core.Middleware{Methods("DELETE"), AuthJWT},
|
||||
},
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/api/users"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package permissions
|
||||
@@ -1 +0,0 @@
|
||||
package permissions
|
||||
@@ -1 +1,41 @@
|
||||
package permissions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandlePermission(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("permission_id")
|
||||
var permission models.Permission
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&permission).
|
||||
Where("id = ?", id).
|
||||
Limit(1).
|
||||
ScanAndCount(context.Background())
|
||||
|
||||
if count == 0 {
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permission not found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permission found.",
|
||||
Data: permission,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -1 +1,36 @@
|
||||
package permissions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
func HandlePermissions(w http.ResponseWriter, r *http.Request) {
|
||||
var permissions core.Permissions
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&permissions).
|
||||
ScanAndCount(context.Background())
|
||||
if count == 0 {
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "No permissions found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permissions found.",
|
||||
Data: permissions,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
38
backend/api/permissions/resource.go
Normal file
38
backend/api/permissions/resource.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package permissions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
func HandlePermissionsResource(w http.ResponseWriter, r *http.Request) {
|
||||
resource := r.PathValue("resource")
|
||||
var permissions core.Permissions
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&permissions).
|
||||
Where("resource = ?", resource).
|
||||
ScanAndCount(context.Background())
|
||||
if count == 0 {
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "No permission with this given resource found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permissions found.",
|
||||
Data: permissions,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package permissions
|
||||
21
backend/api/permissions_routes.go
Normal file
21
backend/api/permissions_routes.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/permissions"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var PermissionsRoutes = map[string]core.Handler{
|
||||
"/permissions": {
|
||||
Handler: permissions.HandlePermissions,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
"/permissions/{permission_id}": {
|
||||
Handler: permissions.HandlePermission,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
"/permissions/resources/{resource}": {
|
||||
Handler: permissions.HandlePermissionsResource,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
}
|
||||
@@ -1 +1,62 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleAddPermission(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
role_id := r.PathValue("role_uuid")
|
||||
permission_id := r.PathValue("permission_id")
|
||||
var permission models.Permission
|
||||
count, err := core.DB.NewSelect().Model(&permission).
|
||||
Where("id = ?", permission_id).
|
||||
Limit(1).ScanAndCount(ctx)
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Permission doesn't exist.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var role models.Role
|
||||
count, err = core.DB.NewSelect().Model(&role).
|
||||
Where("id = ?", role_id).
|
||||
Limit(1).ScanAndCount(ctx)
|
||||
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Role doesn't exist.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
pid, err := strconv.Atoi(permission_id)
|
||||
rid, err := uuid.Parse(role_id)
|
||||
permissionRole := models.PermissionToRole{
|
||||
PermissionID: pid,
|
||||
RoleID: rid,
|
||||
}
|
||||
_, err = core.DB.NewInsert().Model(&permissionRole).Ignore().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permission added.",
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
|
||||
@@ -1 +1,30 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("role_uuid")
|
||||
_, err := core.DB.NewDelete().
|
||||
Model((*models.Role)(nil)).
|
||||
Where("id = ?", uuid).
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Check SQL Result
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Role deteled",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -1 +1,37 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var role models.Role
|
||||
err := json.NewDecoder(r.Body).Decode(&role)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = core.DB.NewInsert().Model(&role).Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Role inserted",
|
||||
Data: role,
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
|
||||
@@ -1 +1,35 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleRolePermissions(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("role_uuid")
|
||||
|
||||
var role models.Role
|
||||
err := core.DB.NewSelect().
|
||||
Model(&role).
|
||||
Where("id = ?", uuid).
|
||||
Relation("Permissions").
|
||||
Limit(1).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permissions.",
|
||||
Data: role.Permissions,
|
||||
}.Respond(w, http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
@@ -1 +1,32 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleRemovePermission(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
role_id := r.PathValue("role_uuid")
|
||||
permission_id := r.PathValue("permission_id")
|
||||
|
||||
_, err := core.DB.NewDelete().Model((*models.PermissionToRole)(nil)).
|
||||
Where("permission_id = ? AND role_id = ?", permission_id, role_id).
|
||||
Exec(ctx)
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Permission removed.",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -1 +1,40 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleRole(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("role_uuid")
|
||||
var role models.Role
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&role).
|
||||
Where("id = ?", uuid).
|
||||
Relation("Permissions").
|
||||
Limit(1).
|
||||
ScanAndCount(context.Background())
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "The role requested was not found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Role found.",
|
||||
Data: role,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -1 +1,39 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleRoles(w http.ResponseWriter, r *http.Request) {
|
||||
var roles []models.Role
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&roles).
|
||||
Relation("Permissions").
|
||||
ScanAndCount(context.Background())
|
||||
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "No role found.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Roles found.",
|
||||
Data: roles,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -1 +1,52 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
var role models.Role
|
||||
err := json.NewDecoder(r.Body).Decode(&role)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
role_uuid := r.PathValue("role_uuid")
|
||||
role.ID, err = uuid.Parse(role_uuid)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = core.DB.NewUpdate().
|
||||
Model(&role).
|
||||
OmitZero().
|
||||
WherePK().
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Event not found.",
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Event updated",
|
||||
Data: role,
|
||||
}.Respond(w, http.StatusOK)
|
||||
|
||||
}
|
||||
|
||||
41
backend/api/roles_routes.go
Normal file
41
backend/api/roles_routes.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/roles"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var RolesRoutes = map[string]core.Handler{
|
||||
"/roles/": {
|
||||
Handler: roles.HandleRoles,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
"/roles/new": {
|
||||
Handler: roles.HandleNew,
|
||||
Middlewares: []core.Middleware{Methods("POST"), AuthJWT},
|
||||
},
|
||||
"/roles/{role_uuid}": {
|
||||
Handler: roles.HandleRole,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
"/roles/{role_uuid}/update": {
|
||||
Handler: roles.HandleUpdate,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
},
|
||||
"/roles/{role_uuid}/delete": {
|
||||
Handler: roles.HandleDelete,
|
||||
Middlewares: []core.Middleware{Methods("DELETE"), AuthJWT},
|
||||
},
|
||||
"/roles/{role_uuid}/permissions/": {
|
||||
Handler: roles.HandleRolePermissions,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
"/roles/{role_uuid}/permissions/{permission_id}/add": {
|
||||
Handler: roles.HandleAddPermission,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
},
|
||||
"/roles/{role_uuid}/permissions/{permission_id}/remove": {
|
||||
Handler: roles.HandleRemovePermission,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
},
|
||||
}
|
||||
@@ -4,13 +4,14 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("shortcode")
|
||||
_, err := core.DB.NewDelete().
|
||||
Model((*core.Shortcode)(nil)).
|
||||
Model((*models.Shortcode)(nil)).
|
||||
Where("code = ?", code).
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
|
||||
@@ -5,11 +5,12 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var shortcode core.Shortcode
|
||||
var shortcode models.Shortcode
|
||||
err := json.NewDecoder(r.Body).Decode(&shortcode)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
|
||||
@@ -4,12 +4,13 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
||||
code := r.PathValue("shortcode")
|
||||
var shortcode core.Shortcode
|
||||
var shortcode models.Shortcode
|
||||
err := core.DB.NewSelect().
|
||||
Model(&shortcode).
|
||||
Where("code = ?", code).
|
||||
|
||||
@@ -4,11 +4,12 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleShortcodes(w http.ResponseWriter, r *http.Request) {
|
||||
var shortcodes []core.Shortcode
|
||||
var shortcodes []models.Shortcode
|
||||
err := core.DB.NewSelect().Model(&shortcodes).Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
|
||||
@@ -8,16 +8,17 @@ import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UpdateShortcodeArgs struct {
|
||||
ID *int64 `json:"id,omitempty"`
|
||||
Code *string `json:"code,omitempty"` // The shortcode value
|
||||
Type *core.ShortcodeType `bun:"shortcode_type" json:"type,omitempty"`
|
||||
Value *string `json:"value,omitempty"`
|
||||
MediaID *uuid.UUID `json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
ID *int64 `json:"id,omitempty"`
|
||||
Code *string `json:"code,omitempty"` // The shortcode value
|
||||
Type *models.ShortcodeType `bun:"shortcode_type" json:"type,omitempty"`
|
||||
Value *string `json:"value,omitempty"`
|
||||
MediaID *uuid.UUID `json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||
}
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -31,7 +32,7 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var shortcode core.Shortcode
|
||||
var shortcode models.Shortcode
|
||||
updateQuery := core.DB.NewUpdate().Model(&shortcode)
|
||||
val := reflect.ValueOf(updateArgs)
|
||||
typ := reflect.TypeOf(updateArgs)
|
||||
|
||||
29
backend/api/shortcodes_routes.go
Normal file
29
backend/api/shortcodes_routes.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/shortcodes"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var ShortcodesRoutes = map[string]core.Handler{
|
||||
"/shortcodes/new": {
|
||||
Handler: shortcodes.HandleNew,
|
||||
Middlewares: []core.Middleware{Methods("POST"), AuthJWT},
|
||||
},
|
||||
"/shortcodes/": {
|
||||
Handler: shortcodes.HandleShortcodes,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT},
|
||||
},
|
||||
"/shortcodes/{shortcode}": {
|
||||
Handler: shortcodes.HandleShortcode,
|
||||
Middlewares: []core.Middleware{Methods("GET")},
|
||||
},
|
||||
"/shortcodes/{shortcode}/delete": {
|
||||
Handler: shortcodes.HandleDelete,
|
||||
Middlewares: []core.Middleware{Methods("DELETE"), AuthJWT},
|
||||
},
|
||||
"/shortcodes/{shortcode}/update": {
|
||||
Handler: shortcodes.HandleUpdate,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT},
|
||||
},
|
||||
}
|
||||
61
backend/api/users/add_role.go
Normal file
61
backend/api/users/add_role.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func HandleAddRole(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
user_id := r.PathValue("user_uuid")
|
||||
role_id := r.PathValue("role_id")
|
||||
var user models.User
|
||||
count, err := core.DB.NewSelect().Model(&user).
|
||||
Where("user_id = ?", user_id).
|
||||
Limit(1).ScanAndCount(ctx)
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "User doesn't exist.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var role models.Role
|
||||
count, err = core.DB.NewSelect().Model(&role).
|
||||
Where("id = ?", role_id).
|
||||
Limit(1).ScanAndCount(ctx)
|
||||
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Role doesn't exist.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
uid, err := uuid.Parse(user_id)
|
||||
rid, err := uuid.Parse(role_id)
|
||||
userRole := models.UserToRole{
|
||||
UserID: uid,
|
||||
RoleID: rid,
|
||||
}
|
||||
_, err = core.DB.NewInsert().Model(&userRole).Ignore().
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Role added.",
|
||||
}.Respond(w, http.StatusCreated)
|
||||
}
|
||||
@@ -8,7 +8,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
@@ -52,7 +53,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := core.Verify(context.Background(), login.Email, login.Password)
|
||||
user, err := models.Verify(core.DB, context.Background(), login.Email, login.Password)
|
||||
if user == nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
|
||||
@@ -4,13 +4,14 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("user_uuid")
|
||||
_, err := core.DB.NewDelete().
|
||||
Model((*core.User)(nil)).
|
||||
Model((*models.User)(nil)).
|
||||
Where("user_id = ?", uuid).
|
||||
Exec(context.Background())
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package users
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
|
||||
@@ -6,11 +6,12 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
core "fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
var user core.User
|
||||
var user models.User
|
||||
err := json.NewDecoder(r.Body).Decode(&user)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
@@ -21,7 +22,7 @@ func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
log.Println("User : ", user)
|
||||
|
||||
res, err := user.Insert(context.Background())
|
||||
res, err := user.Insert(core.DB, context.Background())
|
||||
log.Println(res)
|
||||
// if res == nil {
|
||||
// core.JSONError{
|
||||
|
||||
32
backend/api/users/remove_role.go
Normal file
32
backend/api/users/remove_role.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleRemoveRole(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
user_id := r.PathValue("user_uuid")
|
||||
role_id := r.PathValue("role_id")
|
||||
|
||||
_, err := core.DB.NewDelete().Model((*models.UserToRole)(nil)).
|
||||
Where("user_id = ? AND role_id = ?", user_id, role_id).
|
||||
Exec(ctx)
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Role removed.",
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
31
backend/api/users/roles.go
Normal file
31
backend/api/users/roles.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleRoles(w http.ResponseWriter, r *http.Request) {
|
||||
user_id := r.PathValue("user_uuid")
|
||||
var user models.User
|
||||
err := core.DB.NewSelect().Model(&user).Where("user_id = ?", user_id).
|
||||
Relation("Roles").
|
||||
Limit(1).
|
||||
Scan(context.Background())
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Roles found.",
|
||||
Data: user.Roles,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
@@ -9,16 +9,17 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
type UpdateUserArgs struct {
|
||||
FirstName *string `json:"firstname,omitempty"`
|
||||
LastName *string `json:"lastname,omitempty"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Phone *string `json:"phone,omitempty"`
|
||||
Attributes *core.UserAttributes `json:"attributes"`
|
||||
FirstName *string `json:"firstname,omitempty"`
|
||||
LastName *string `json:"lastname,omitempty"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Phone *string `json:"phone,omitempty"`
|
||||
Attributes *models.UserAttributes `json:"attributes"`
|
||||
}
|
||||
|
||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -32,7 +33,7 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var user core.User
|
||||
var user models.User
|
||||
updateQuery := core.DB.NewUpdate().Model(&user)
|
||||
|
||||
val := reflect.ValueOf(updateArgs)
|
||||
|
||||
@@ -4,15 +4,17 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
)
|
||||
|
||||
func HandleUser(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := r.PathValue("user_uuid")
|
||||
var user core.User
|
||||
var user models.User
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&user).
|
||||
Where("user_id = ?", uuid).
|
||||
Relation("Roles").
|
||||
Limit(1).
|
||||
ScanAndCount(context.Background())
|
||||
|
||||
|
||||
@@ -4,17 +4,19 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/core"
|
||||
"fr.latosa-escrima/core/models"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleUsers(w http.ResponseWriter, r *http.Request) {
|
||||
var users []core.User
|
||||
var users []models.User
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&users).
|
||||
Relation("Roles").
|
||||
ScanAndCount(context.Background())
|
||||
|
||||
users = utils.Map(users, func(user core.User) core.User {
|
||||
users = utils.Map(users, func(user models.User) models.User {
|
||||
user.Password = ""
|
||||
return user
|
||||
})
|
||||
|
||||
39
backend/api/users_routes.go
Normal file
39
backend/api/users_routes.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fr.latosa-escrima/api/users"
|
||||
"fr.latosa-escrima/core"
|
||||
)
|
||||
|
||||
var UserRoutes = map[string]core.Handler{
|
||||
"/users/login": {
|
||||
Handler: users.HandleLogin,
|
||||
Middlewares: []core.Middleware{Methods("POST")}},
|
||||
"/users/me": {
|
||||
Handler: users.HandleMe,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT}},
|
||||
"/users": {
|
||||
Handler: users.HandleUsers,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT}},
|
||||
"/users/new": {
|
||||
Handler: users.HandleNew,
|
||||
Middlewares: []core.Middleware{Methods("POST"), AuthJWT}},
|
||||
"/users/{user_uuid}": {
|
||||
Handler: users.HandleUser,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT}},
|
||||
"/users/{user_uuid}/delete": {
|
||||
Handler: users.HandleDelete,
|
||||
Middlewares: []core.Middleware{Methods("DELETE"), AuthJWT}},
|
||||
"/users/{user_uuid}/update": {
|
||||
Handler: users.HandleUpdate,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT}},
|
||||
"/users/{user_uuid}/roles": {
|
||||
Handler: users.HandleRoles,
|
||||
Middlewares: []core.Middleware{Methods("GET"), AuthJWT}},
|
||||
"/users/{user_uuid}/roles/{role_id}/add": {
|
||||
Handler: users.HandleAddRole,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT}},
|
||||
"/users/{user_uuid}/roles/{role_id}/remove": {
|
||||
Handler: users.HandleRemoveRole,
|
||||
Middlewares: []core.Middleware{Methods("PATCH"), AuthJWT}},
|
||||
}
|
||||
Reference in New Issue
Block a user