full stack first commit
This commit is contained in:
@@ -45,6 +45,16 @@ type Membership struct {
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Activity struct {
|
||||
ID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
FileID *string
|
||||
Action string
|
||||
Metadata map[string]interface{}
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
func (db *DB) GetOrCreateUser(ctx context.Context, sub, email, name string) (*User, error) {
|
||||
var user User
|
||||
err := db.QueryRowContext(ctx, `
|
||||
@@ -122,3 +132,89 @@ func (db *DB) GetUserMembership(ctx context.Context, userID, orgID uuid.UUID) (*
|
||||
}
|
||||
return &membership, nil
|
||||
}
|
||||
|
||||
func (db *DB) CreateOrg(ctx context.Context, name, slug string) (*Organization, error) {
|
||||
var org Organization
|
||||
err := db.QueryRowContext(ctx, `
|
||||
INSERT INTO organizations (name, slug)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, name, slug, created_at
|
||||
`, name, slug).Scan(&org.ID, &org.Name, &org.Slug, &org.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &org, nil
|
||||
}
|
||||
|
||||
func (db *DB) AddMembership(ctx context.Context, userID, orgID uuid.UUID, role string) error {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
INSERT INTO memberships (user_id, org_id, role)
|
||||
VALUES ($1, $2, $3)
|
||||
`, userID, orgID, role)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) LogActivity(ctx context.Context, userID, orgID uuid.UUID, fileID *string, action string, metadata map[string]interface{}) error {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
INSERT INTO activities (user_id, org_id, file_id, action, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`, userID, orgID, fileID, action, metadata)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetOrgActivities(ctx context.Context, orgID uuid.UUID, limit int) ([]Activity, error) {
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT id, user_id, org_id, file_id, action, metadata, timestamp
|
||||
FROM activities
|
||||
WHERE org_id = $1
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT $2
|
||||
`, orgID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var activities []Activity
|
||||
for rows.Next() {
|
||||
var a Activity
|
||||
err := rows.Scan(&a.ID, &a.UserID, &a.OrgID, &a.FileID, &a.Action, &a.Metadata, &a.Timestamp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
activities = append(activities, a)
|
||||
}
|
||||
return activities, rows.Err()
|
||||
}
|
||||
|
||||
func (db *DB) GetOrgMembers(ctx context.Context, orgID uuid.UUID) ([]Membership, error) {
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT user_id, org_id, role, created_at
|
||||
FROM memberships
|
||||
WHERE org_id = $1
|
||||
`, orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var memberships []Membership
|
||||
for rows.Next() {
|
||||
var m Membership
|
||||
err := rows.Scan(&m.UserID, &m.OrgID, &m.Role, &m.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
memberships = append(memberships, m)
|
||||
}
|
||||
return memberships, rows.Err()
|
||||
}
|
||||
|
||||
func (db *DB) UpdateMemberRole(ctx context.Context, orgID, userID uuid.UUID, role string) error {
|
||||
_, err := db.ExecContext(ctx, `
|
||||
UPDATE memberships
|
||||
SET role = $1
|
||||
WHERE org_id = $2 AND user_id = $3
|
||||
`, role, orgID, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user