Skip to content

Commit

Permalink
Add update and creat fields in schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Jul 4, 2024
1 parent 2e2d9a8 commit b8a06cf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied.

CREATE TABLE users
(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE cards
(
id SERIAL PRIMARY KEY,
front TEXT NOT NULL,
back TEXT NOT NULL,
review_date TIMESTAMP NOT NULL,
interval_days INT NOT NULL DEFAULT 1,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE
OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated
= CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;

CREATE TRIGGER update_users_updated_at
BEFORE UPDATE
ON users
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

CREATE TRIGGER update_cards_updated_at
BEFORE UPDATE
ON cards
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back.

DROP TRIGGER IF EXISTS update_users_updated_at ON users;
DROP TRIGGER IF EXISTS update_cards_updated_at ON cards;
DROP FUNCTION IF EXISTS update_updated_at_column;
DROP TABLE IF EXISTS cards;
DROP TABLE IF EXISTS users;

This file was deleted.

13 changes: 13 additions & 0 deletions backend/pkg/flashcard/card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package flashcard

import "time"

type Card struct {
ID uint `gorm:"primaryKey"`
Front string `gorm:"not null"`
Back string `gorm:"not null"`
ReviewDate time.Time `gorm:"not null"`
IntervalDays int `gorm:"not null;default:1"`
Created time.Time `gorm:"autoCreateTime"`
Updated time.Time `gorm:"autoUpdateTime"`
}
9 changes: 0 additions & 9 deletions backend/pkg/flashcard/flashcard_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import (
"time"
)

// Card struct represents a flashcard with necessary fields
type Card struct {
ID uint `gorm:"primaryKey"`
Front string `gorm:"not null"`
Back string `gorm:"not null"`
ReviewDate time.Time `gorm:"not null"`
IntervalDays int `gorm:"not null;default:1"`
}

// GetDueCards retrieves flashcards that are due for review
func GetDueCards(db *gorm.DB) ([]Card, error) {
var cards []Card
Expand Down
12 changes: 12 additions & 0 deletions backend/pkg/flashcard/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package flashcard

import (
"time"
)

type User struct {
ID string `gorm:"primaryKey"`
Name string `gorm:"not null"`
Created time.Time `gorm:"autoCreateTime"`
Updated time.Time `gorm:"autoUpdateTime"`
}

0 comments on commit b8a06cf

Please sign in to comment.