Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: init user schema #2

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions deliveries/http/handlers/user/user_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handlers

import service "github.com/dezh-tech/panda/services/user"

type User struct {
service service.User
}

func NewUserService(userSvc service.User) User {
return User{
service: userSvc,
}
}
12 changes: 6 additions & 6 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const docTemplate = `{
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/domainhandler.DomainGetResponse"
"$ref": "#/definitions/handlers.DomainGetResponse"
}
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ const docTemplate = `{
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/domainhandler.DomainCreateRequest"
"$ref": "#/definitions/handlers.DomainCreateRequest"
}
}
],
Expand All @@ -102,7 +102,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/domainhandler.DomainCreateResponse"
"$ref": "#/definitions/handlers.DomainCreateResponse"
}
}
}
Expand All @@ -126,7 +126,7 @@ const docTemplate = `{
}
},
"definitions": {
"domainhandler.DomainCreateRequest": {
"handlers.DomainCreateRequest": {
"type": "object",
"required": [
"base_price_per_identifier",
Expand Down Expand Up @@ -155,13 +155,13 @@ const docTemplate = `{
}
}
},
"domainhandler.DomainCreateResponse": {
"handlers.DomainCreateResponse": {
"type": "object",
"properties": {
"id": {}
}
},
"domainhandler.DomainGetResponse": {
"handlers.DomainGetResponse": {
"type": "object",
"properties": {
"base_price_per_identifier": {
Expand Down
12 changes: 6 additions & 6 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/domainhandler.DomainGetResponse"
"$ref": "#/definitions/handlers.DomainGetResponse"
}
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/domainhandler.DomainCreateRequest"
"$ref": "#/definitions/handlers.DomainCreateRequest"
}
}
],
Expand All @@ -96,7 +96,7 @@
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/domainhandler.DomainCreateResponse"
"$ref": "#/definitions/handlers.DomainCreateResponse"
}
}
}
Expand All @@ -120,7 +120,7 @@
}
},
"definitions": {
"domainhandler.DomainCreateRequest": {
"handlers.DomainCreateRequest": {
"type": "object",
"required": [
"base_price_per_identifier",
Expand Down Expand Up @@ -149,13 +149,13 @@
}
}
},
"domainhandler.DomainCreateResponse": {
"handlers.DomainCreateResponse": {
"type": "object",
"properties": {
"id": {}
}
},
"domainhandler.DomainGetResponse": {
"handlers.DomainGetResponse": {
"type": "object",
"properties": {
"base_price_per_identifier": {
Expand Down
12 changes: 6 additions & 6 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
basePath: /
definitions:
domainhandler.DomainCreateRequest:
handlers.DomainCreateRequest:
properties:
base_price_per_identifier:
minimum: 1
Expand All @@ -21,11 +21,11 @@ definitions:
- domain
- status
type: object
domainhandler.DomainCreateResponse:
handlers.DomainCreateResponse:
properties:
id: {}
type: object
domainhandler.DomainGetResponse:
handlers.DomainGetResponse:
properties:
base_price_per_identifier:
type: integer
Expand Down Expand Up @@ -93,7 +93,7 @@ paths:
- properties:
data:
items:
$ref: '#/definitions/domainhandler.DomainGetResponse'
$ref: '#/definitions/handlers.DomainGetResponse'
type: array
type: object
"500":
Expand All @@ -113,7 +113,7 @@ paths:
name: domain
required: true
schema:
$ref: '#/definitions/domainhandler.DomainCreateRequest'
$ref: '#/definitions/handlers.DomainCreateRequest'
produces:
- application/json
responses:
Expand All @@ -124,7 +124,7 @@ paths:
- $ref: '#/definitions/pkg.ResponseDto'
- properties:
data:
$ref: '#/definitions/domainhandler.DomainCreateResponse'
$ref: '#/definitions/handlers.DomainCreateResponse'
type: object
"400":
description: Bad Request - Validation error
Expand Down
62 changes: 62 additions & 0 deletions repositories/user_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package repositories

import (
"context"
"errors"
"time"

schema "github.com/dezh-tech/panda/schemas"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

type User struct {
*Base
}

func NewUserRepository(client *mongo.Client, dbName string, timeout time.Duration) *User {
return &User{
Base: NewBaseRepository(client, dbName, schema.UserSchemaName, timeout),
}
}

func (r *User) Add(ctx context.Context, d *schema.User) (*mongo.InsertOneResult, error) {
d.CreatedAt = time.Now()
d.UpdatedAt = time.Now()

return r.InsertOne(ctx, d)
}

func (r *User) GetByField(ctx context.Context,
fieldName string, value interface{},
) (*schema.User, error) {
var result *schema.User
err := r.FindOne(ctx, bson.M{fieldName: value}, result)
if err != nil {
return nil, err
}

return result, nil
}

func (r *User) GetAll(ctx context.Context, filter interface{}) (*[]schema.User, error) {
results := new([]schema.User)
err := r.FindAll(ctx, filter, results)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return &[]schema.User{}, nil
}

return nil, err
}

return results, nil
}

func (r *User) Update(ctx context.Context, filter, update interface{}) (*mongo.UpdateResult, error) {
return r.UpdateOne(ctx, filter, update)
}

func (r *User) Delete(ctx context.Context, filter interface{}) (*mongo.DeleteResult, error) {
return r.DeleteOne(ctx, filter)
}
24 changes: 1 addition & 23 deletions schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
```json
{
"_id": "ObjectId",
"name": "Users",
"npub": "string", // Nostr public key
"passwordHash": "string", // Hashed password for authentication
"pubKey": "string", // Nostr public key
"createdAt": "ISODate", // User creation timestamp
"updatedAt": "ISODate" // Last updated timestamp
}
Expand All @@ -19,7 +17,6 @@
**Description**: Defines domain settings, including pricing and TTL values.
```json
{
"name": "Domains",
"_id": "ObjectId",
"domain": "string", // Domain name (e.g., "example.com")
"basePricePerIdentifier": "number", // Base cost per identifier (in sats)
Expand All @@ -36,7 +33,6 @@
**Description**: Tracks identifiers assigned to users within a domain.
```json
{
"name": "Identifiers",
"_id": "ObjectId",
"name": "string", // Identifier name (e.g., "alice")
"domainId": "ObjectId", // Reference to Domains table
Expand All @@ -50,24 +46,6 @@

---

### **Schema: Transactions**
**Description**: Logs all transactions, including payments for identifiers or domains.
```json
{
"name": "Transactions",
"_id": "ObjectId",
"userId": "ObjectId", // Reference to Users table
"domainId": "ObjectId", // Reference to Domains table
"identifierId": "ObjectId", // Reference to Identifiers table
"amount": "number", // Amount paid
"currency": "string", // e.g., "BTC", "USD"
"status": "string", // e.g., "completed", "pending"
"createdAt": "ISODate" // Transaction creation timestamp
}
```

---

### **Schema: Records**
**Description**: Stores resolution records (e.g., TXT, NOSTR, CNAME) for identifiers.
```json
Expand Down
8 changes: 0 additions & 8 deletions schemas/base.go

This file was deleted.

14 changes: 9 additions & 5 deletions schemas/domain_schema.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package schema

import "time"

const DomainSchemaName = "domains"

type Domain struct {
Domain string `bson:"Domain"`
BasePricePerIdentifier uint `bson:"BasePricePerIdentifier"`
DefaultTTL uint32 `bson:"DefaultTTL"`
Status string `bson:"Status"`
Domain string `bson:"domain"`
BasePricePerIdentifier uint `bson:"base_price_per_identifier"`
DefaultTTL uint32 `bson:"default_ttl"`
Status string `bson:"status"`

Base
ID interface{} `bson:"_id"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
}
13 changes: 13 additions & 0 deletions schemas/user_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package schema

import "time"

const UserSchemaName = "users"

type User struct {
PubKey string `bson:"pubkey"`

ID interface{} `bson:"_id"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
}
28 changes: 28 additions & 0 deletions services/user/user_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package service

import (
"context"

schema "github.com/dezh-tech/panda/schemas"
)

func (s User) Create(ctx context.Context, pubKey string) (interface{}, error) {
// Check if the user already exists
d, err := s.repo.GetByField(ctx, "pubkey", pubKey)
if err != nil {
return nil, err
}

if d != nil {
return nil, err
}

id, err := s.repo.Add(ctx, &schema.User{
PubKey: pubKey,
})
if err != nil {
return nil, err
}

return id.InsertedID, nil
}
9 changes: 9 additions & 0 deletions services/user/user_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package service

import "errors"

var (
Err = errors.New("something went wrong")
ErrNotFound = errors.New("user not found")
ErrIsExist = errors.New("user exist")
)
26 changes: 26 additions & 0 deletions services/user/user_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package service

import (
"context"

schema "github.com/dezh-tech/panda/schemas"
"go.mongodb.org/mongo-driver/bson"
)

func (s User) GetAll(ctx context.Context, filter interface{}) (*[]schema.User, error) {
Users, err := s.repo.GetAll(ctx, filter)
if err != nil {
return nil, err
}

return Users, nil
}

func (s User) GetAllWithoutFilter(ctx context.Context) (*[]schema.User, error) {
Users, err := s.GetAll(ctx, bson.M{})
if err != nil {
return nil, err
}

return Users, nil
}
Loading
Loading