resolving conflicts
This commit is contained in:
@@ -2,21 +2,32 @@ package blogs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
<<<<<<< HEAD
|
||||||
"fmt"
|
"fmt"
|
||||||
|
=======
|
||||||
|
>>>>>>> dev/cedric
|
||||||
"net/http"
|
"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) {
|
func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
blog_uuid := r.PathValue("uuid")
|
blog_uuid := r.PathValue("uuid")
|
||||||
|
|
||||||
var blog core.Blog
|
var blog models.Blog
|
||||||
_, err := core.DB.NewSelect().
|
_, err := core.DB.NewSelect().
|
||||||
Model(&blog).
|
Model(&blog).
|
||||||
Where("blog_id = ?", blog_uuid).
|
Where("blog_id = ?", blog_uuid).
|
||||||
|
>>>>>>> dev/cedric
|
||||||
Relation("Author").
|
Relation("Author").
|
||||||
ScanAndCount(context.Background())
|
ScanAndCount(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -29,11 +40,17 @@ func HandleBlog(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
core.JSONSuccess{
|
core.JSONSuccess{
|
||||||
Status: core.Success,
|
Status: core.Success,
|
||||||
|
<<<<<<< HEAD
|
||||||
|
Message: fmt.Sprintf("%d blogs objects sent", count),
|
||||||
|
=======
|
||||||
Message: "Status OK",
|
Message: "Status OK",
|
||||||
|
>>>>>>> dev/cedric
|
||||||
Data: blog,
|
Data: blog,
|
||||||
}.Respond(w, http.StatusOK)
|
}.Respond(w, http.StatusOK)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<<<<<<<< HEAD:backend/api/blogs/blog.go
|
||||||
|
|
||||||
func HandleBlogs(w http.ResponseWriter, r *http.Request) {
|
func HandleBlogs(w http.ResponseWriter, r *http.Request) {
|
||||||
var blog []core.Blog
|
var blog []core.Blog
|
||||||
@@ -56,3 +73,7 @@ func HandleBlogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusOK)
|
}.Respond(w, http.StatusOK)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
========
|
||||||
|
>>>>>>>> dev/cedric:backend/api/blogs/blogs.go
|
||||||
|
=======
|
||||||
|
>>>>>>> dev/cedric
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package blogs
|
package blogs
|
||||||
|
<<<<<<< HEAD
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -30,3 +31,5 @@ func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
|||||||
Message: "Blog deleted.",
|
Message: "Blog deleted.",
|
||||||
}.Respond(w, http.StatusOK)
|
}.Respond(w, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
=======
|
||||||
|
>>>>>>> dev/cedric
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ package blogs
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
|
||||||
"io"
|
"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) {
|
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -17,7 +19,7 @@ func HandleNew(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusNoContent)
|
}.Respond(w, http.StatusNoContent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var blog core.Blog
|
var blog models.Blog
|
||||||
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&blog); err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
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"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
"gopkg.in/gomail.v2"
|
"gopkg.in/gomail.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
uuid := r.PathValue("event_uuid")
|
uuid := r.PathValue("event_uuid")
|
||||||
var event core.Event
|
var event models.Event
|
||||||
res, err := core.DB.NewDelete().
|
res, err := core.DB.NewDelete().
|
||||||
Model(&event).
|
Model(&event).
|
||||||
Where("event_id = ?", uuid).
|
Where("event_id = ?", uuid).
|
||||||
|
|||||||
@@ -2,15 +2,15 @@ package events
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"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) {
|
func HandleEvent(w http.ResponseWriter, r *http.Request) {
|
||||||
event_uuid := r.PathValue("event_uuid")
|
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())
|
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -27,22 +27,3 @@ func HandleEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusOK)
|
}.Respond(w, http.StatusOK)
|
||||||
return
|
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"
|
"encoding/json"
|
||||||
"net/http"
|
"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) {
|
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||||
var event core.Event
|
var event models.Event
|
||||||
err := json.NewDecoder(r.Body).Decode(&event)
|
err := json.NewDecoder(r.Body).Decode(&event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||||
var event core.Event
|
var event models.Event
|
||||||
err := json.NewDecoder(r.Body).Decode(&event)
|
err := json.NewDecoder(r.Body).Decode(&event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -32,6 +33,7 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Println(event)
|
log.Println(event)
|
||||||
|
|
||||||
_, err = core.DB.NewUpdate().
|
_, err = core.DB.NewUpdate().
|
||||||
Model(&event).
|
Model(&event).
|
||||||
OmitZero().
|
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"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
"github.com/gorilla/csrf"
|
"github.com/gorilla/csrf"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
uuid := r.PathValue("media_uuid")
|
uuid := r.PathValue("media_uuid")
|
||||||
var media core.Media
|
var media models.Media
|
||||||
res, err := core.DB.NewDelete().
|
res, err := core.DB.NewDelete().
|
||||||
Model(&media).
|
Model(&media).
|
||||||
Where("id = ?", uuid).
|
Where("id = ?", uuid).
|
||||||
|
|||||||
@@ -7,7 +7,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
"fr.latosa-escrima/utils"
|
"fr.latosa-escrima/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,12 +33,12 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
|||||||
offset := (page - 1) * limit
|
offset := (page - 1) * limit
|
||||||
|
|
||||||
total, err := core.DB.NewSelect().
|
total, err := core.DB.NewSelect().
|
||||||
Model((*core.Media)(nil)).
|
Model((*models.Media)(nil)).
|
||||||
Count(context.Background())
|
Count(context.Background())
|
||||||
|
|
||||||
totalPages := int(math.Max(1, float64(total/limit)))
|
totalPages := int(math.Max(1, float64(total/limit)))
|
||||||
|
|
||||||
var media []core.Media
|
var media []models.Media
|
||||||
err = core.DB.NewSelect().
|
err = core.DB.NewSelect().
|
||||||
Model(&media).
|
Model(&media).
|
||||||
Limit(limit).
|
Limit(limit).
|
||||||
@@ -51,7 +52,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
baseURL := utils.GetURL(r)
|
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.Author = nil
|
||||||
m.URL = fmt.Sprintf("%s%s/file", baseURL, m.ID)
|
m.URL = fmt.Sprintf("%s%s/file", baseURL, m.ID)
|
||||||
return m
|
return m
|
||||||
@@ -60,7 +61,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
|||||||
core.JSONSuccess{
|
core.JSONSuccess{
|
||||||
Status: core.Success,
|
Status: core.Success,
|
||||||
Message: "Media successfully retrieved",
|
Message: "Media successfully retrieved",
|
||||||
Data: core.Paginated[core.Media]{
|
Data: core.Paginated[models.Media]{
|
||||||
Page: page,
|
Page: page,
|
||||||
Limit: limit,
|
Limit: limit,
|
||||||
TotalPages: totalPages,
|
TotalPages: totalPages,
|
||||||
@@ -71,7 +72,7 @@ func HandleMedia(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
||||||
uuid := r.PathValue("media_uuid")
|
uuid := r.PathValue("media_uuid")
|
||||||
var media core.Media
|
var media models.Media
|
||||||
err := core.DB.NewSelect().
|
err := core.DB.NewSelect().
|
||||||
Model(&media).
|
Model(&media).
|
||||||
Where("id = ?", uuid).
|
Where("id = ?", uuid).
|
||||||
@@ -98,7 +99,7 @@ func HandleMediaDetails(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func HandleMediaFile(w http.ResponseWriter, r *http.Request) {
|
func HandleMediaFile(w http.ResponseWriter, r *http.Request) {
|
||||||
uuid := r.PathValue("media_uuid")
|
uuid := r.PathValue("media_uuid")
|
||||||
var media core.Media
|
var media models.Media
|
||||||
err := core.DB.NewSelect().
|
err := core.DB.NewSelect().
|
||||||
Model(&media).
|
Model(&media).
|
||||||
Where("id = ?", uuid).
|
Where("id = ?", uuid).
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
"fr.latosa-escrima/utils"
|
"fr.latosa-escrima/utils"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -95,7 +96,7 @@ func HandleUpload(w http.ResponseWriter, r *http.Request) {
|
|||||||
}.Respond(w, http.StatusInternalServerError)
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
media := &core.Media{
|
media := &models.Media{
|
||||||
AuthorID: id,
|
AuthorID: id,
|
||||||
Type: fileHeader.Header.Get("Content-Type"),
|
Type: fileHeader.Header.Get("Content-Type"),
|
||||||
Alt: "To be implemented",
|
Alt: "To be implemented",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
"fr.latosa-escrima/utils"
|
"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"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
|
||||||
"fr.latosa-escrima/api/users"
|
"fr.latosa-escrima/api/users"
|
||||||
|
core "fr.latosa-escrima/core"
|
||||||
"fr.latosa-escrima/utils"
|
"fr.latosa-escrima/utils"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
package permissions
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
package permissions
|
|
||||||
@@ -1 +1,41 @@
|
|||||||
package permissions
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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"
|
"context"
|
||||||
"net/http"
|
"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) {
|
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
code := r.PathValue("shortcode")
|
code := r.PathValue("shortcode")
|
||||||
_, err := core.DB.NewDelete().
|
_, err := core.DB.NewDelete().
|
||||||
Model((*core.Shortcode)(nil)).
|
Model((*models.Shortcode)(nil)).
|
||||||
Where("code = ?", code).
|
Where("code = ?", code).
|
||||||
Exec(context.Background())
|
Exec(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||||
var shortcode core.Shortcode
|
var shortcode models.Shortcode
|
||||||
err := json.NewDecoder(r.Body).Decode(&shortcode)
|
err := json.NewDecoder(r.Body).Decode(&shortcode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
func HandleShortcode(w http.ResponseWriter, r *http.Request) {
|
||||||
code := r.PathValue("shortcode")
|
code := r.PathValue("shortcode")
|
||||||
var shortcode core.Shortcode
|
var shortcode models.Shortcode
|
||||||
err := core.DB.NewSelect().
|
err := core.DB.NewSelect().
|
||||||
Model(&shortcode).
|
Model(&shortcode).
|
||||||
Where("code = ?", code).
|
Where("code = ?", code).
|
||||||
|
|||||||
@@ -4,11 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleShortcodes(w http.ResponseWriter, r *http.Request) {
|
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())
|
err := core.DB.NewSelect().Model(&shortcodes).Scan(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
|
|||||||
@@ -8,16 +8,17 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateShortcodeArgs struct {
|
type UpdateShortcodeArgs struct {
|
||||||
ID *int64 `json:"id,omitempty"`
|
ID *int64 `json:"id,omitempty"`
|
||||||
Code *string `json:"code,omitempty"` // The shortcode value
|
Code *string `json:"code,omitempty"` // The shortcode value
|
||||||
Type *core.ShortcodeType `bun:"shortcode_type" json:"type,omitempty"`
|
Type *models.ShortcodeType `bun:"shortcode_type" json:"type,omitempty"`
|
||||||
Value *string `json:"value,omitempty"`
|
Value *string `json:"value,omitempty"`
|
||||||
MediaID *uuid.UUID `json:"media_id,omitempty"` // Nullable reference to another table's ID
|
MediaID *uuid.UUID `json:"media_id,omitempty"` // Nullable reference to another table's ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -31,7 +32,7 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var shortcode core.Shortcode
|
var shortcode models.Shortcode
|
||||||
updateQuery := core.DB.NewUpdate().Model(&shortcode)
|
updateQuery := core.DB.NewUpdate().Model(&shortcode)
|
||||||
val := reflect.ValueOf(updateArgs)
|
val := reflect.ValueOf(updateArgs)
|
||||||
typ := reflect.TypeOf(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"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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 {
|
if user == nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
func HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
uuid := r.PathValue("user_uuid")
|
uuid := r.PathValue("user_uuid")
|
||||||
_, err := core.DB.NewDelete().
|
_, err := core.DB.NewDelete().
|
||||||
Model((*core.User)(nil)).
|
Model((*models.User)(nil)).
|
||||||
Where("user_id = ?", uuid).
|
Where("user_id = ?", uuid).
|
||||||
Exec(context.Background())
|
Exec(context.Background())
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package users
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,12 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"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) {
|
func HandleNew(w http.ResponseWriter, r *http.Request) {
|
||||||
var user core.User
|
var user models.User
|
||||||
err := json.NewDecoder(r.Body).Decode(&user)
|
err := json.NewDecoder(r.Body).Decode(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
@@ -21,7 +22,7 @@ func HandleNew(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
log.Println("User : ", user)
|
log.Println("User : ", user)
|
||||||
|
|
||||||
res, err := user.Insert(context.Background())
|
res, err := user.Insert(core.DB, context.Background())
|
||||||
log.Println(res)
|
log.Println(res)
|
||||||
// if res == nil {
|
// if res == nil {
|
||||||
// core.JSONError{
|
// 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"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateUserArgs struct {
|
type UpdateUserArgs struct {
|
||||||
FirstName *string `json:"firstname,omitempty"`
|
FirstName *string `json:"firstname,omitempty"`
|
||||||
LastName *string `json:"lastname,omitempty"`
|
LastName *string `json:"lastname,omitempty"`
|
||||||
Email *string `json:"email,omitempty"`
|
Email *string `json:"email,omitempty"`
|
||||||
Password *string `json:"password,omitempty"`
|
Password *string `json:"password,omitempty"`
|
||||||
Phone *string `json:"phone,omitempty"`
|
Phone *string `json:"phone,omitempty"`
|
||||||
Attributes *core.UserAttributes `json:"attributes"`
|
Attributes *models.UserAttributes `json:"attributes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -32,7 +33,7 @@ func HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var user core.User
|
var user models.User
|
||||||
updateQuery := core.DB.NewUpdate().Model(&user)
|
updateQuery := core.DB.NewUpdate().Model(&user)
|
||||||
|
|
||||||
val := reflect.ValueOf(updateArgs)
|
val := reflect.ValueOf(updateArgs)
|
||||||
|
|||||||
@@ -4,15 +4,17 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleUser(w http.ResponseWriter, r *http.Request) {
|
func HandleUser(w http.ResponseWriter, r *http.Request) {
|
||||||
uuid := r.PathValue("user_uuid")
|
uuid := r.PathValue("user_uuid")
|
||||||
var user core.User
|
var user models.User
|
||||||
count, err := core.DB.NewSelect().
|
count, err := core.DB.NewSelect().
|
||||||
Model(&user).
|
Model(&user).
|
||||||
Where("user_id = ?", uuid).
|
Where("user_id = ?", uuid).
|
||||||
|
Relation("Roles").
|
||||||
Limit(1).
|
Limit(1).
|
||||||
ScanAndCount(context.Background())
|
ScanAndCount(context.Background())
|
||||||
|
|
||||||
|
|||||||
@@ -4,17 +4,19 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/core"
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
"fr.latosa-escrima/utils"
|
"fr.latosa-escrima/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleUsers(w http.ResponseWriter, r *http.Request) {
|
func HandleUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
var users []core.User
|
var users []models.User
|
||||||
count, err := core.DB.NewSelect().
|
count, err := core.DB.NewSelect().
|
||||||
Model(&users).
|
Model(&users).
|
||||||
|
Relation("Roles").
|
||||||
ScanAndCount(context.Background())
|
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 = ""
|
user.Password = ""
|
||||||
return user
|
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}},
|
||||||
|
}
|
||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"fr.latosa-escrima/api/core"
|
|
||||||
"fr.latosa-escrima/cmd/migrate/migrations"
|
"fr.latosa-escrima/cmd/migrate/migrations"
|
||||||
|
"fr.latosa-escrima/core"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/uptrace/bun/dialect/pgdialect"
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||||||
"github.com/uptrace/bun/driver/pgdriver"
|
"github.com/uptrace/bun/driver/pgdriver"
|
||||||
|
|||||||
24
backend/core/models/blogs.go
Normal file
24
backend/core/models/blogs.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
25
backend/core/models/events.go
Normal file
25
backend/core/models/events.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Status string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Active Status = "Active"
|
||||||
|
Inactive Status = "Inactive"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
16
backend/core/models/events_to_user.go
Normal file
16
backend/core/models/events_to_user.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
8
backend/core/models/main.go
Normal file
8
backend/core/models/main.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type Group string
|
||||||
|
|
||||||
|
const (
|
||||||
|
LatosaGroup Group = "latosa"
|
||||||
|
WingTsunGroup Group = "wing-tsun"
|
||||||
|
)
|
||||||
18
backend/core/models/media.go
Normal file
18
backend/core/models/media.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
15
backend/core/models/permissions.go
Normal file
15
backend/core/models/permissions.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/uptrace/bun"
|
||||||
|
|
||||||
|
type PermissionConditions struct {
|
||||||
|
Groups *[]Group `json:"groups,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Permission struct {
|
||||||
|
bun.BaseModel `bun:"table:permissions"`
|
||||||
|
ID int `bun:"id,pk,autoincrement" json:"id"`
|
||||||
|
Resource string `bun:"resource,notnull,unique:permission" json:"resource"`
|
||||||
|
Action string `bun:"action,notnull,unique:permission" json:"action"`
|
||||||
|
Conditions PermissionConditions `bun:"conditions,type:jsonb" json:"conditions"`
|
||||||
|
}
|
||||||
16
backend/core/models/permissions_to_roles.go
Normal file
16
backend/core/models/permissions_to_roles.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
14
backend/core/models/roles.go
Normal file
14
backend/core/models/roles.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
|
||||||
|
Permissions []Permission `bun:"m2m:permissions_to_users,join:Role=Permission" json:"permissions,omitempty"`
|
||||||
|
}
|
||||||
36
backend/core/models/shortcodes.go
Normal file
36
backend/core/models/shortcodes.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
60
backend/core/models/users.go
Normal file
60
backend/core/models/users.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserAttributes struct {
|
||||||
|
Groups []Group `json:"groups"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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"`
|
||||||
|
Roles []Role `bun:"m2m:users_to_roles,join:User=Role" json:"roles,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) Insert(db *bun.DB, 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(db *bun.DB, 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
|
||||||
|
}
|
||||||
16
backend/core/models/users_to_roles.go
Normal file
16
backend/core/models/users_to_roles.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
13
backend/core/models/website_settings.go
Normal file
13
backend/core/models/website_settings.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
}
|
||||||
61
backend/core/permissions.go
Normal file
61
backend/core/permissions.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"fr.latosa-escrima/core/models"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Permissions []models.Permission
|
||||||
|
|
||||||
|
func GetAllPermissions() Permissions {
|
||||||
|
resources := []string{"users", "roles", "media", "events", "permissions", "shortcodes", "blogs"}
|
||||||
|
var perms Permissions
|
||||||
|
for _, resource := range resources {
|
||||||
|
perms = append(perms, Permissions{
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "insert",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "update",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "delete",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "get",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "own:insert",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "own:update",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "own:delete",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Resource: resource,
|
||||||
|
Action: "own:get",
|
||||||
|
},
|
||||||
|
}...)
|
||||||
|
}
|
||||||
|
return perms
|
||||||
|
}
|
||||||
|
|
||||||
|
func (perms Permissions) InsertAll(db *bun.DB, ctx context.Context) error {
|
||||||
|
_, err := db.NewInsert().
|
||||||
|
Model(&perms).
|
||||||
|
Ignore().
|
||||||
|
Exec(ctx)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
64
backend/core/schemas.go
Normal file
64
backend/core/schemas.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||||||
|
"github.com/uptrace/bun/driver/pgdriver"
|
||||||
|
|
||||||
|
m "fr.latosa-escrima/core/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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((*m.EventToUser)(nil))
|
||||||
|
db.RegisterModel((*m.PermissionToRole)(nil))
|
||||||
|
db.RegisterModel((*m.UserToRole)(nil))
|
||||||
|
_, err = db.NewCreateTable().Model((*m.User)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.Event)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.EventToUser)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.Blog)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.WebsiteSettings)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.Media)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.Shortcode)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.Role)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.Permission)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.PermissionToRole)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
_, err = db.NewCreateTable().Model((*m.UserToRole)(nil)).IfNotExists().Exec(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
perms := GetAllPermissions()
|
||||||
|
err = perms.InsertAll(db, ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
166
backend/main.go
166
backend/main.go
@@ -12,12 +12,8 @@ import (
|
|||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
"fr.latosa-escrima/api"
|
"fr.latosa-escrima/api"
|
||||||
"fr.latosa-escrima/api/blogs"
|
"fr.latosa-escrima/core"
|
||||||
"fr.latosa-escrima/api/core"
|
"fr.latosa-escrima/utils"
|
||||||
"fr.latosa-escrima/api/events"
|
|
||||||
"fr.latosa-escrima/api/media"
|
|
||||||
"fr.latosa-escrima/api/shortcodes"
|
|
||||||
"fr.latosa-escrima/api/users"
|
|
||||||
"github.com/gorilla/csrf"
|
"github.com/gorilla/csrf"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -68,154 +64,10 @@ func main() {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
core.HandleRoutes(mux, map[string]core.Handler{
|
baseRoutes := map[string]core.Handler{
|
||||||
"/": {
|
"/": {
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
Middlewares: []core.Middleware{api.Methods("get")}},
|
Middlewares: []core.Middleware{api.Methods("get")}},
|
||||||
"/users/login": {
|
|
||||||
Handler: users.HandleLogin,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("POST")}},
|
|
||||||
"/users/me": {
|
|
||||||
Handler: users.HandleMe,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
|
||||||
"/users": {
|
|
||||||
Handler: users.HandleUsers,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
|
||||||
"/users/new": {
|
|
||||||
Handler: users.HandleNew,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT}},
|
|
||||||
"/users/{user_uuid}": {
|
|
||||||
Handler: users.HandleUser,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
|
||||||
"/users/{user_uuid}/delete": {
|
|
||||||
Handler: users.HandleDelete,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT}},
|
|
||||||
"/users/{user_uuid}/update": {
|
|
||||||
Handler: users.HandleUpdate,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
|
||||||
"/events": {
|
|
||||||
Handler: events.HandleEvents,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
|
||||||
"/events/new": {
|
|
||||||
Handler: events.HandleNew,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("POST")}},
|
|
||||||
"/events/{event_uuid}": {
|
|
||||||
Handler: events.HandleEvent,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
|
||||||
"/events/{event_uuid}/delete": {
|
|
||||||
Handler: events.HandleDelete,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("DELETE")}},
|
|
||||||
"/events/{event_uuid}/update": {
|
|
||||||
Handler: events.HandleUpdate,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("PATCH")}},
|
|
||||||
"/blogs/new": {
|
|
||||||
Handler: blogs.HandleNew,
|
|
||||||
Middlewares: []core.Middleware{api.Methods(("POST"))}},
|
|
||||||
"/blogs/{uuid}": {
|
|
||||||
Handler: blogs.HandleBlog,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
|
||||||
"/media/upload": {
|
|
||||||
Handler: media.HandleUpload,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT}},
|
|
||||||
"/media/verify": {
|
|
||||||
Handler: media.HandleVerify,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
|
||||||
},
|
|
||||||
// Paginated media response
|
|
||||||
"/media/": {
|
|
||||||
Handler: media.HandleMedia,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")},
|
|
||||||
},
|
|
||||||
// Unique element
|
|
||||||
"/media/{media_uuid}": {
|
|
||||||
Handler: media.HandleMediaDetails,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")},
|
|
||||||
},
|
|
||||||
// Get the image, video, GIF etc.
|
|
||||||
"/media/{media_uuid}/file": {
|
|
||||||
Handler: media.HandleMediaFile,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")},
|
|
||||||
},
|
|
||||||
// "/media/{media_uuid}/update": {
|
|
||||||
// Handler: api.HandleGetMediaFile,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
"/media/{media_uuid}/delete": {
|
|
||||||
Handler: media.HandleDelete,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT},
|
|
||||||
},
|
|
||||||
"/shortcodes/new": {
|
|
||||||
Handler: shortcodes.HandleNew,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
|
||||||
},
|
|
||||||
"/shortcodes/": {
|
|
||||||
Handler: shortcodes.HandleShortcodes,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT},
|
|
||||||
},
|
|
||||||
"/shortcodes/{shortcode}": {
|
|
||||||
Handler: shortcodes.HandleShortcode,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")},
|
|
||||||
},
|
|
||||||
"/shortcodes/{shortcode}/delete": {
|
|
||||||
Handler: shortcodes.HandleDelete,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT},
|
|
||||||
},
|
|
||||||
"/shortcodes/{shortcode}/update": {
|
|
||||||
Handler: shortcodes.HandleUpdate,
|
|
||||||
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT},
|
|
||||||
},
|
|
||||||
// "/roles": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/new": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/{role_uuid}": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/{role_uuid}/update": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/{role_uuid}/delete": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/permissions": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/permissions/new": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/permissions/{permission_id}": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/permissions/{permission_id}/update": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/permissions/{permission_id}/delete": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("DELETE"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/{role_uuid}/permissions/": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/{role_uuid}/permissions/{permission_id}/add": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
// "/roles/{role_uuid}/permissions/{permission_id}/remove": {
|
|
||||||
// Handler: nil,
|
|
||||||
// Middlewares: []core.Middleware{api.Methods("POST"), api.AuthJWT},
|
|
||||||
// },
|
|
||||||
"/contact": {
|
"/contact": {
|
||||||
Handler: api.HandleContact,
|
Handler: api.HandleContact,
|
||||||
Middlewares: []core.Middleware{api.Methods("POST"), CSRFMiddleware},
|
Middlewares: []core.Middleware{api.Methods("POST"), CSRFMiddleware},
|
||||||
@@ -224,7 +76,17 @@ func main() {
|
|||||||
Handler: api.HandleCSRF,
|
Handler: api.HandleCSRF,
|
||||||
Middlewares: []core.Middleware{api.Methods("GET"), CSRFMiddleware},
|
Middlewares: []core.Middleware{api.Methods("GET"), CSRFMiddleware},
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
routes := utils.MergeMaps(
|
||||||
|
baseRoutes,
|
||||||
|
api.UserRoutes,
|
||||||
|
api.BlogsRoutes,
|
||||||
|
api.EventsRoutes,
|
||||||
|
api.MediaRoutes,
|
||||||
|
api.PermissionsRoutes,
|
||||||
|
api.RolesRoutes,
|
||||||
|
api.ShortcodesRoutes)
|
||||||
|
core.HandleRoutes(mux, routes)
|
||||||
|
|
||||||
fmt.Printf("Serving on port %s\n", port)
|
fmt.Printf("Serving on port %s\n", port)
|
||||||
err = http.ListenAndServe(fmt.Sprintf(":%s", port), api.CORS(mux))
|
err = http.ListenAndServe(fmt.Sprintf(":%s", port), api.CORS(mux))
|
||||||
|
|||||||
13
backend/utils/merge_maps.go
Normal file
13
backend/utils/merge_maps.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
merged := make(map[K]V)
|
||||||
|
|
||||||
|
for _, m := range maps {
|
||||||
|
for k, v := range m {
|
||||||
|
merged[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import Planning from "@/components/planning.tsx";
|
import Planning from "@/components/planning";
|
||||||
import { useApi } from "@/hooks/use-api";
|
import { useApi } from "@/hooks/use-api";
|
||||||
import { type CalendarEventExternal } from "@schedule-x/calendar";
|
import { type CalendarEventExternal } from "@schedule-x/calendar";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ const Planning: React.FC<{
|
|||||||
setEventSelected(event);
|
setEventSelected(event);
|
||||||
},
|
},
|
||||||
async onEventUpdate(event) {
|
async onEventUpdate(event) {
|
||||||
|
console.log(event);
|
||||||
await handleEventUpdate(event);
|
await handleEventUpdate(event);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Reference in New Issue
Block a user