2024-10-16 04:34:57 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-11-11 19:55:01 +00:00
|
|
|
"time"
|
2024-10-16 04:34:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Guestbook struct {
|
2024-11-11 19:55:01 +00:00
|
|
|
ID int64
|
|
|
|
ShortId uint64
|
2024-10-16 04:34:57 +00:00
|
|
|
SiteUrl string
|
2024-11-11 19:55:01 +00:00
|
|
|
UserId int64
|
|
|
|
Created time.Time
|
2024-10-16 04:34:57 +00:00
|
|
|
IsDeleted bool
|
|
|
|
IsActive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type GuestbookModel struct {
|
|
|
|
DB *sql.DB
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:55:01 +00:00
|
|
|
func (m *GuestbookModel) Insert(shortId uint64, siteUrl string, userId int64) (int64, error) {
|
|
|
|
stmt := `INSERT INTO guestbooks (ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive)
|
|
|
|
VALUES(?, ?, ?, ?, FALSE, TRUE)`
|
|
|
|
result, err := m.DB.Exec(stmt, shortId, siteUrl, userId, time.Now().UTC())
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
id, err := result.LastInsertId()
|
2024-10-16 04:34:57 +00:00
|
|
|
if err != nil {
|
2024-11-11 19:55:01 +00:00
|
|
|
return -1, err
|
2024-10-16 04:34:57 +00:00
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:55:01 +00:00
|
|
|
func (m *GuestbookModel) Get(shortId uint64) (Guestbook, error) {
|
|
|
|
stmt := `SELECT Id, ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive FROM guestbooks
|
|
|
|
WHERE ShortId = ?`
|
|
|
|
row := m.DB.QueryRow(stmt, shortId)
|
2024-10-16 04:34:57 +00:00
|
|
|
var g Guestbook
|
2024-11-11 19:55:01 +00:00
|
|
|
err := row.Scan(&g.ID, &g.ShortId, &g.SiteUrl, &g.UserId, &g.Created, &g.IsDeleted, &g.IsActive)
|
2024-10-16 04:34:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return Guestbook{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2024-11-11 19:55:01 +00:00
|
|
|
func (m *GuestbookModel) GetAll(userId int64) ([]Guestbook, error) {
|
|
|
|
stmt := `SELECT Id, ShortId, SiteUrl, UserId, Created, IsDeleted, IsActive FROM guestbooks
|
2024-10-23 05:25:30 +00:00
|
|
|
WHERE UserId = ?`
|
|
|
|
rows, err := m.DB.Query(stmt, userId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var guestbooks []Guestbook
|
|
|
|
for rows.Next() {
|
|
|
|
var g Guestbook
|
2024-11-11 19:55:01 +00:00
|
|
|
err = rows.Scan(&g.ID, &g.ShortId, &g.SiteUrl, &g.UserId, &g.Created, &g.IsDeleted, &g.IsActive)
|
2024-10-23 05:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
guestbooks = append(guestbooks, g)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return guestbooks, nil
|
2024-10-16 04:34:57 +00:00
|
|
|
}
|