Add full shop application: Go backend, PostgreSQL, Docker, Caddy and admin panel.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
cookieName = "shop_session"
|
||||
sessionTTL = 24 * time.Hour
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
secret []byte
|
||||
}
|
||||
|
||||
func NewSession(secret string) *Session {
|
||||
return &Session{secret: []byte(secret)}
|
||||
}
|
||||
|
||||
func (s *Session) Create(email string) (string, error) {
|
||||
exp := time.Now().Add(sessionTTL).Unix()
|
||||
payload := fmt.Sprintf("%s|%d", email, exp)
|
||||
sig := s.sign(payload)
|
||||
token := base64.URLEncoding.EncodeToString([]byte(payload + "|" + sig))
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (s *Session) Validate(token string) (string, bool) {
|
||||
raw, err := base64.URLEncoding.DecodeString(token)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
parts := strings.Split(string(raw), "|")
|
||||
if len(parts) != 3 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
email, expStr, sig := parts[0], parts[1], parts[2]
|
||||
payload := email + "|" + expStr
|
||||
|
||||
if !hmac.Equal([]byte(sig), []byte(s.sign(payload))) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
exp, err := strconv.ParseInt(expStr, 10, 64)
|
||||
if err != nil || time.Now().Unix() > exp {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return email, true
|
||||
}
|
||||
|
||||
func (s *Session) SetCookie(w http.ResponseWriter, token string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: cookieName,
|
||||
Value: token,
|
||||
Path: "/admin",
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
MaxAge: int(sessionTTL.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Session) ClearCookie(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: cookieName,
|
||||
Value: "",
|
||||
Path: "/admin",
|
||||
HttpOnly: true,
|
||||
MaxAge: -1,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Session) FromRequest(r *http.Request) (string, bool) {
|
||||
c, err := r.Cookie(cookieName)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return s.Validate(c.Value)
|
||||
}
|
||||
|
||||
func (s *Session) sign(payload string) string {
|
||||
mac := hmac.New(sha256.New, s.secret)
|
||||
mac.Write([]byte(payload))
|
||||
return base64.URLEncoding.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AdminEmail string
|
||||
AdminPassword string
|
||||
SessionSecret string
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
cfg := Config{
|
||||
AdminEmail: os.Getenv("ADMIN_EMAIL"),
|
||||
AdminPassword: os.Getenv("ADMIN_PASSWORD"),
|
||||
SessionSecret: os.Getenv("SESSION_SECRET"),
|
||||
}
|
||||
|
||||
if cfg.SessionSecret == "" {
|
||||
cfg.SessionSecret = randomSecret()
|
||||
log.Println("warning: SESSION_SECRET not set, using random value (sessions reset on restart)")
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func randomSecret() string {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func Connect(ctx context.Context) (*pgxpool.Pool, error) {
|
||||
dsn := fmt.Sprintf(
|
||||
"postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
||||
getEnv("DB_USER", "shop"),
|
||||
getEnv("DB_PASSWORD", "shop_secret"),
|
||||
getEnv("DB_HOST", "localhost"),
|
||||
getEnv("DB_PORT", "5432"),
|
||||
getEnv("DB_NAME", "shop"),
|
||||
)
|
||||
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse config: %w", err)
|
||||
}
|
||||
|
||||
cfg.MaxConns = 10
|
||||
cfg.MinConns = 2
|
||||
cfg.MaxConnLifetime = time.Hour
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect: %w", err)
|
||||
}
|
||||
|
||||
for i := 0; i < 30; i++ {
|
||||
if err := pool.Ping(ctx); err == nil {
|
||||
return pool, nil
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("database not ready after 30s")
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
_, err := pool.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS admins (
|
||||
id SERIAL PRIMARY KEY,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
`)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"shop/internal/auth"
|
||||
"shop/internal/repository"
|
||||
)
|
||||
|
||||
type AdminHandler struct {
|
||||
repo *repository.AdminRepository
|
||||
products *repository.ProductRepository
|
||||
session *auth.Session
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
type adminLoginData struct {
|
||||
Title string
|
||||
Error string
|
||||
Email string
|
||||
}
|
||||
|
||||
type adminDashboardData struct {
|
||||
Title string
|
||||
Email string
|
||||
ProductCount int
|
||||
Categories interface{}
|
||||
}
|
||||
|
||||
func NewAdminHandler(
|
||||
adminRepo *repository.AdminRepository,
|
||||
productRepo *repository.ProductRepository,
|
||||
session *auth.Session,
|
||||
templates *template.Template,
|
||||
) *AdminHandler {
|
||||
return &AdminHandler{
|
||||
repo: adminRepo,
|
||||
products: productRepo,
|
||||
session: session,
|
||||
templates: templates,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AdminHandler) LoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
if email, ok := h.session.FromRequest(r); ok && email != "" {
|
||||
http.Redirect(w, r, "/admin/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
data := adminLoginData{Title: "Вход в админку"}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_ = h.templates.ExecuteTemplate(w, "admin_login.html", data)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
email := r.FormValue("email")
|
||||
password := r.FormValue("password")
|
||||
|
||||
ok, err := h.repo.Authenticate(r.Context(), email, password)
|
||||
if err != nil {
|
||||
log.Printf("admin auth: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
h.renderLoginError(w, email, "Неверный email или пароль")
|
||||
return
|
||||
}
|
||||
|
||||
token, err := h.session.Create(email)
|
||||
if err != nil {
|
||||
log.Printf("session: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
h.session.SetCookie(w, token)
|
||||
http.Redirect(w, r, "/admin/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
h.session.ClearCookie(w)
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
email, ok := h.session.FromRequest(r)
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
products, err := h.products.Featured(ctx, 100)
|
||||
if err != nil {
|
||||
log.Printf("admin products: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := h.products.Categories(ctx)
|
||||
if err != nil {
|
||||
log.Printf("admin categories: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := adminDashboardData{
|
||||
Title: "Админ-панель",
|
||||
Email: email,
|
||||
ProductCount: len(products),
|
||||
Categories: categories,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_ = h.templates.ExecuteTemplate(w, "admin_dashboard.html", data)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) renderLoginError(w http.ResponseWriter, email, msg string) {
|
||||
data := adminLoginData{
|
||||
Title: "Вход в админку",
|
||||
Error: msg,
|
||||
Email: email,
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
_ = h.templates.ExecuteTemplate(w, "admin_login.html", data)
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"shop/internal/repository"
|
||||
)
|
||||
|
||||
//go:embed templates/*
|
||||
var templateFS embed.FS
|
||||
|
||||
//go:embed static/*
|
||||
var staticFS embed.FS
|
||||
|
||||
type HomeHandler struct {
|
||||
repo *repository.ProductRepository
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
type homePageData struct {
|
||||
Title string
|
||||
Products interface{}
|
||||
Categories interface{}
|
||||
}
|
||||
|
||||
func NewHomeHandler(repo *repository.ProductRepository) (*HomeHandler, error) {
|
||||
tmpl, err := template.ParseFS(templateFS, "templates/*.html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &HomeHandler{repo: repo, templates: tmpl}, nil
|
||||
}
|
||||
|
||||
func (h *HomeHandler) Templates() *template.Template {
|
||||
return h.templates
|
||||
}
|
||||
|
||||
func (h *HomeHandler) Static() http.Handler {
|
||||
sub, err := fs.Sub(staticFS, "static")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return http.FileServer(http.FS(sub))
|
||||
}
|
||||
|
||||
func (h *HomeHandler) Home(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
products, err := h.repo.Featured(ctx, 8)
|
||||
if err != nil {
|
||||
log.Printf("featured products: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := h.repo.Categories(ctx)
|
||||
if err != nil {
|
||||
log.Printf("categories: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := homePageData{
|
||||
Title: "Shop — Интернет-магазин",
|
||||
Products: products,
|
||||
Categories: categories,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := h.templates.ExecuteTemplate(w, "home.html", data); err != nil {
|
||||
log.Printf("render: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HomeHandler) Health(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"status":"ok"}`))
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
.admin-body {
|
||||
background: #f3f4f6;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.admin-login {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.admin-login__card {
|
||||
background: #fff;
|
||||
border-radius: 16px;
|
||||
padding: 40px;
|
||||
width: 100%;
|
||||
max-width: 420px;
|
||||
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.admin-login__logo {
|
||||
justify-content: center;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.admin-login__title {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.admin-login__back {
|
||||
display: block;
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
color: #6b7280;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.admin-login__back:hover {
|
||||
color: #4f46e5;
|
||||
}
|
||||
|
||||
.admin-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.admin-form__label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: #374151;
|
||||
}
|
||||
|
||||
.admin-form__input {
|
||||
padding: 12px 14px;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 10px;
|
||||
font-size: 1rem;
|
||||
font-family: inherit;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.admin-form__input:focus {
|
||||
outline: none;
|
||||
border-color: #4f46e5;
|
||||
box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
|
||||
}
|
||||
|
||||
.admin-form__submit {
|
||||
width: 100%;
|
||||
justify-content: center;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.admin-alert {
|
||||
padding: 12px 16px;
|
||||
border-radius: 10px;
|
||||
font-size: 0.9rem;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.admin-alert--error {
|
||||
background: #fef2f2;
|
||||
color: #b91c1c;
|
||||
border: 1px solid #fecaca;
|
||||
}
|
||||
|
||||
.admin-header {
|
||||
background: #fff;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.admin-header__inner {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
height: 64px;
|
||||
}
|
||||
|
||||
.admin-header__user {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
font-size: 0.9rem;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.admin-main {
|
||||
padding: 40px 24px;
|
||||
}
|
||||
|
||||
.admin-page-title {
|
||||
font-size: 1.75rem;
|
||||
font-weight: 700;
|
||||
margin-bottom: 32px;
|
||||
}
|
||||
|
||||
.admin-stats {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||
gap: 20px;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.admin-stat {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 24px;
|
||||
border: 1px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.admin-stat__value {
|
||||
display: block;
|
||||
font-size: 2rem;
|
||||
font-weight: 700;
|
||||
color: #4f46e5;
|
||||
}
|
||||
|
||||
.admin-stat__label {
|
||||
font-size: 0.875rem;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.admin-section {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 24px;
|
||||
border: 1px solid #e5e7eb;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.admin-section__title {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.admin-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.admin-table th,
|
||||
.admin-table td {
|
||||
text-align: left;
|
||||
padding: 12px;
|
||||
border-bottom: 1px solid #f3f4f6;
|
||||
}
|
||||
|
||||
.admin-table th {
|
||||
font-size: 0.8rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.admin-hint {
|
||||
font-size: 0.875rem;
|
||||
color: #9ca3af;
|
||||
}
|
||||
|
||||
.admin-hint code {
|
||||
background: #e5e7eb;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
@@ -0,0 +1,495 @@
|
||||
*,
|
||||
*::before,
|
||||
*::after {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
:root {
|
||||
--color-bg: #fafafa;
|
||||
--color-surface: #ffffff;
|
||||
--color-text: #1a1a2e;
|
||||
--color-text-muted: #6b7280;
|
||||
--color-primary: #4f46e5;
|
||||
--color-primary-hover: #4338ca;
|
||||
--color-border: #e5e7eb;
|
||||
--color-hero-bg: linear-gradient(135deg, #eef2ff 0%, #faf5ff 50%, #fdf2f8 100%);
|
||||
--radius: 12px;
|
||||
--radius-lg: 20px;
|
||||
--shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
|
||||
--shadow-md: 0 4px 20px rgba(0, 0, 0, 0.08);
|
||||
--font: 'Inter', system-ui, -apple-system, sans-serif;
|
||||
}
|
||||
|
||||
html {
|
||||
scroll-behavior: smooth;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: var(--font);
|
||||
background: var(--color-bg);
|
||||
color: var(--color-text);
|
||||
line-height: 1.6;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 0 24px;
|
||||
}
|
||||
|
||||
/* Header */
|
||||
.header {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
background: rgba(255, 255, 255, 0.9);
|
||||
backdrop-filter: blur(12px);
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.header__inner {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
height: 72px;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.logo {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
text-decoration: none;
|
||||
color: var(--color-text);
|
||||
font-weight: 700;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.logo__icon {
|
||||
color: var(--color-primary);
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.nav {
|
||||
display: flex;
|
||||
gap: 32px;
|
||||
}
|
||||
|
||||
.nav__link {
|
||||
text-decoration: none;
|
||||
color: var(--color-text-muted);
|
||||
font-weight: 500;
|
||||
font-size: 0.95rem;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.nav__link:hover {
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.header__actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
/* Buttons */
|
||||
.btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 10px 20px;
|
||||
border-radius: var(--radius);
|
||||
font-family: var(--font);
|
||||
font-weight: 600;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
text-decoration: none;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn--primary {
|
||||
background: var(--color-primary);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn--primary:hover {
|
||||
background: var(--color-primary-hover);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.btn--ghost {
|
||||
background: transparent;
|
||||
color: var(--color-text-muted);
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.btn--ghost:hover {
|
||||
color: var(--color-primary);
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.btn--outline {
|
||||
background: transparent;
|
||||
color: var(--color-text);
|
||||
border: 2px solid var(--color-border);
|
||||
}
|
||||
|
||||
.btn--outline:hover {
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.btn--lg {
|
||||
padding: 14px 28px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.btn--sm {
|
||||
padding: 8px 16px;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
/* Hero */
|
||||
.hero {
|
||||
background: var(--color-hero-bg);
|
||||
padding: 80px 0 100px;
|
||||
}
|
||||
|
||||
.hero__inner {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 60px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 6px 14px;
|
||||
background: rgba(79, 70, 229, 0.1);
|
||||
color: var(--color-primary);
|
||||
border-radius: 100px;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.hero__title {
|
||||
font-size: clamp(2.5rem, 5vw, 3.5rem);
|
||||
font-weight: 700;
|
||||
line-height: 1.15;
|
||||
letter-spacing: -0.02em;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.hero__text {
|
||||
font-size: 1.125rem;
|
||||
color: var(--color-text-muted);
|
||||
max-width: 480px;
|
||||
margin-bottom: 32px;
|
||||
}
|
||||
|
||||
.hero__cta {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.hero__visual {
|
||||
position: relative;
|
||||
height: 320px;
|
||||
}
|
||||
|
||||
.hero__card {
|
||||
position: absolute;
|
||||
background: var(--color-surface);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 24px 32px;
|
||||
box-shadow: var(--shadow-md);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.hero__card--1 {
|
||||
top: 40px;
|
||||
right: 20px;
|
||||
}
|
||||
|
||||
.hero__card--2 {
|
||||
bottom: 40px;
|
||||
left: 20px;
|
||||
}
|
||||
|
||||
.hero__card-label {
|
||||
font-size: 0.85rem;
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
.hero__card-value {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
/* Sections */
|
||||
.section {
|
||||
padding: 80px 0;
|
||||
}
|
||||
|
||||
.section--alt {
|
||||
background: var(--color-surface);
|
||||
}
|
||||
|
||||
.section__header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.section__title {
|
||||
font-size: 2rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
|
||||
.link {
|
||||
color: var(--color-primary);
|
||||
text-decoration: none;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* Categories */
|
||||
.categories {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.category-card {
|
||||
background: var(--color-surface);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius);
|
||||
padding: 24px;
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.category-card:hover {
|
||||
border-color: var(--color-primary);
|
||||
box-shadow: var(--shadow-md);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.category-card__name {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
font-size: 1.1rem;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.category-card__count {
|
||||
font-size: 0.85rem;
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
/* Products */
|
||||
.products {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.product-card {
|
||||
background: var(--color-bg);
|
||||
border-radius: var(--radius-lg);
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--color-border);
|
||||
transition: all 0.25s;
|
||||
}
|
||||
|
||||
.product-card:hover {
|
||||
box-shadow: var(--shadow-md);
|
||||
transform: translateY(-4px);
|
||||
}
|
||||
|
||||
.product-card__image {
|
||||
position: relative;
|
||||
aspect-ratio: 4/3;
|
||||
overflow: hidden;
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.product-card__image img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
transition: transform 0.3s;
|
||||
}
|
||||
|
||||
.product-card:hover .product-card__image img {
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
.product-card__badge {
|
||||
position: absolute;
|
||||
top: 12px;
|
||||
left: 12px;
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
padding: 4px 10px;
|
||||
border-radius: 100px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.product-card__body {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.product-card__title {
|
||||
font-size: 1.05rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.product-card__desc {
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-text-muted);
|
||||
margin-bottom: 16px;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.product-card__footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.product-card__price {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.empty {
|
||||
grid-column: 1 / -1;
|
||||
text-align: center;
|
||||
color: var(--color-text-muted);
|
||||
padding: 48px;
|
||||
}
|
||||
|
||||
/* Features */
|
||||
.features {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
|
||||
gap: 32px;
|
||||
}
|
||||
|
||||
.feature {
|
||||
text-align: center;
|
||||
padding: 32px 24px;
|
||||
background: var(--color-surface);
|
||||
border-radius: var(--radius-lg);
|
||||
border: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.feature__icon {
|
||||
font-size: 2.5rem;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.feature__title {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.feature__text {
|
||||
font-size: 0.9rem;
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
/* Footer */
|
||||
.footer {
|
||||
background: var(--color-text);
|
||||
color: #9ca3af;
|
||||
padding: 48px 0;
|
||||
}
|
||||
|
||||
.footer__inner {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
flex-wrap: wrap;
|
||||
gap: 32px;
|
||||
}
|
||||
|
||||
.footer__brand .logo__icon,
|
||||
.footer__brand .logo__text {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.footer__copy {
|
||||
margin-top: 12px;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.footer__links {
|
||||
display: flex;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.footer__links a {
|
||||
color: #9ca3af;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.footer__links a:hover {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
/* Responsive */
|
||||
@media (max-width: 768px) {
|
||||
.nav {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.hero__inner {
|
||||
grid-template-columns: 1fr;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.hero__text {
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
||||
.hero__cta {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.hero__visual {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.section__header {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 12px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
{{define "admin_dashboard.html"}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Title}}</title>
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
<link rel="stylesheet" href="/static/css/admin.css">
|
||||
</head>
|
||||
<body class="admin-body">
|
||||
<header class="admin-header">
|
||||
<div class="container admin-header__inner">
|
||||
<a href="/admin/" class="logo">
|
||||
<span class="logo__icon">◆</span>
|
||||
<span class="logo__text">Shop Admin</span>
|
||||
</a>
|
||||
<div class="admin-header__user">
|
||||
<span>{{.Email}}</span>
|
||||
<form method="POST" action="/admin/logout">
|
||||
<button type="submit" class="btn btn--ghost btn--sm">Выйти</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="admin-main container">
|
||||
<h1 class="admin-page-title">Панель управления</h1>
|
||||
|
||||
<div class="admin-stats">
|
||||
<div class="admin-stat">
|
||||
<span class="admin-stat__value">{{.ProductCount}}</span>
|
||||
<span class="admin-stat__label">Товаров в каталоге</span>
|
||||
</div>
|
||||
<div class="admin-stat">
|
||||
<span class="admin-stat__value">{{len .Categories}}</span>
|
||||
<span class="admin-stat__label">Категорий</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{if .Categories}}
|
||||
<section class="admin-section">
|
||||
<h2 class="admin-section__title">Категории</h2>
|
||||
<table class="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Название</th>
|
||||
<th>Товаров</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Categories}}
|
||||
<tr>
|
||||
<td>{{.Name}}</td>
|
||||
<td>{{.Count}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
{{end}}
|
||||
|
||||
<p class="admin-hint">Администратор создаётся автоматически из переменных <code>ADMIN_EMAIL</code> и <code>ADMIN_PASSWORD</code> в <code>.env</code>.</p>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
@@ -0,0 +1,40 @@
|
||||
{{define "admin_login.html"}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Title}}</title>
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
<link rel="stylesheet" href="/static/css/admin.css">
|
||||
</head>
|
||||
<body class="admin-body">
|
||||
<div class="admin-login">
|
||||
<div class="admin-login__card">
|
||||
<a href="/" class="logo admin-login__logo">
|
||||
<span class="logo__icon">◆</span>
|
||||
<span class="logo__text">Shop Admin</span>
|
||||
</a>
|
||||
<h1 class="admin-login__title">Вход в админку</h1>
|
||||
|
||||
{{if .Error}}
|
||||
<div class="admin-alert admin-alert--error">{{.Error}}</div>
|
||||
{{end}}
|
||||
|
||||
<form method="POST" action="/admin/login" class="admin-form">
|
||||
<label class="admin-form__label">
|
||||
Email
|
||||
<input type="email" name="email" class="admin-form__input" value="{{.Email}}" required autofocus>
|
||||
</label>
|
||||
<label class="admin-form__label">
|
||||
Пароль
|
||||
<input type="password" name="password" class="admin-form__input" required>
|
||||
</label>
|
||||
<button type="submit" class="btn btn--primary btn--lg admin-form__submit">Войти</button>
|
||||
</form>
|
||||
<a href="/" class="admin-login__back">← На главную</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
@@ -0,0 +1,150 @@
|
||||
{{define "home.html"}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Title}}</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header class="header">
|
||||
<div class="container header__inner">
|
||||
<a href="/" class="logo">
|
||||
<span class="logo__icon">◆</span>
|
||||
<span class="logo__text">Shop</span>
|
||||
</a>
|
||||
<nav class="nav">
|
||||
<a href="#catalog" class="nav__link">Каталог</a>
|
||||
<a href="#categories" class="nav__link">Категории</a>
|
||||
<a href="#delivery" class="nav__link">Доставка</a>
|
||||
</nav>
|
||||
<div class="header__actions">
|
||||
<button class="btn btn--ghost" aria-label="Поиск">
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.35-4.35"/></svg>
|
||||
</button>
|
||||
<button class="btn btn--primary">
|
||||
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 0 1-8 0"/></svg>
|
||||
Корзина
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section class="hero">
|
||||
<div class="container hero__inner">
|
||||
<div class="hero__content">
|
||||
<span class="badge">Новая коллекция 2026</span>
|
||||
<h1 class="hero__title">Всё лучшее —<br>в одном магазине</h1>
|
||||
<p class="hero__text">Электроника, одежда, дом и сад. Быстрая доставка по всей стране, гарантия качества и удобная оплата.</p>
|
||||
<div class="hero__cta">
|
||||
<a href="#catalog" class="btn btn--primary btn--lg">Смотреть каталог</a>
|
||||
<a href="#delivery" class="btn btn--outline btn--lg">Условия доставки</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hero__visual">
|
||||
<div class="hero__card hero__card--1">
|
||||
<span class="hero__card-label">Бесплатная доставка</span>
|
||||
<span class="hero__card-value">от 3 000 ₽</span>
|
||||
</div>
|
||||
<div class="hero__card hero__card--2">
|
||||
<span class="hero__card-label">Товаров в каталоге</span>
|
||||
<span class="hero__card-value">10 000+</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{{if .Categories}}
|
||||
<section id="categories" class="section">
|
||||
<div class="container">
|
||||
<h2 class="section__title">Популярные категории</h2>
|
||||
<div class="categories">
|
||||
{{range .Categories}}
|
||||
<a href="#catalog" class="category-card">
|
||||
<span class="category-card__name">{{.Name}}</span>
|
||||
<span class="category-card__count">{{.Count}} товаров</span>
|
||||
</a>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{{end}}
|
||||
|
||||
<section id="catalog" class="section section--alt">
|
||||
<div class="container">
|
||||
<div class="section__header">
|
||||
<h2 class="section__title">Популярные товары</h2>
|
||||
<a href="#" class="link">Весь каталог →</a>
|
||||
</div>
|
||||
<div class="products">
|
||||
{{range .Products}}
|
||||
<article class="product-card">
|
||||
<div class="product-card__image">
|
||||
<img src="{{.ImageURL}}" alt="{{.Name}}" loading="lazy">
|
||||
<span class="product-card__badge">{{.Category}}</span>
|
||||
</div>
|
||||
<div class="product-card__body">
|
||||
<h3 class="product-card__title">{{.Name}}</h3>
|
||||
<p class="product-card__desc">{{.Description}}</p>
|
||||
<div class="product-card__footer">
|
||||
<span class="product-card__price">{{printf "%.0f" .Price}} ₽</span>
|
||||
<button class="btn btn--primary btn--sm">В корзину</button>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
{{else}}
|
||||
<p class="empty">Товары скоро появятся в каталоге.</p>
|
||||
{{end}}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section id="delivery" class="section">
|
||||
<div class="container">
|
||||
<h2 class="section__title">Почему выбирают нас</h2>
|
||||
<div class="features">
|
||||
<div class="feature">
|
||||
<div class="feature__icon">🚚</div>
|
||||
<h3 class="feature__title">Быстрая доставка</h3>
|
||||
<p class="feature__text">Доставим заказ за 1–3 дня в любой город России</p>
|
||||
</div>
|
||||
<div class="feature">
|
||||
<div class="feature__icon">🔒</div>
|
||||
<h3 class="feature__title">Безопасная оплата</h3>
|
||||
<p class="feature__text">Карты, СБП и оплата при получении</p>
|
||||
</div>
|
||||
<div class="feature">
|
||||
<div class="feature__icon">↩️</div>
|
||||
<h3 class="feature__title">Лёгкий возврат</h3>
|
||||
<p class="feature__text">14 дней на возврат без лишних вопросов</p>
|
||||
</div>
|
||||
<div class="feature">
|
||||
<div class="feature__icon">💬</div>
|
||||
<h3 class="feature__title">Поддержка 24/7</h3>
|
||||
<p class="feature__text">Ответим на любой вопрос в чате или по телефону</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<footer class="footer">
|
||||
<div class="container footer__inner">
|
||||
<div class="footer__brand">
|
||||
<span class="logo__icon">◆</span>
|
||||
<span class="logo__text">Shop</span>
|
||||
<p class="footer__copy">© 2026 Shop. Все права защищены.</p>
|
||||
</div>
|
||||
<div class="footer__links">
|
||||
<a href="#">О компании</a>
|
||||
<a href="#">Контакты</a>
|
||||
<a href="#">Политика конфиденциальности</a>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
@@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Product struct {
|
||||
ID int
|
||||
Name string
|
||||
Description string
|
||||
Price float64
|
||||
ImageURL string
|
||||
Category string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
Name string
|
||||
Slug string
|
||||
Count int
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type AdminRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAdminRepository(pool *pgxpool.Pool) *AdminRepository {
|
||||
return &AdminRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *AdminRepository) Upsert(ctx context.Context, email, password string) error {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.pool.Exec(ctx, `
|
||||
INSERT INTO admins (email, password_hash)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (email) DO UPDATE SET password_hash = EXCLUDED.password_hash
|
||||
`, email, string(hash))
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *AdminRepository) Authenticate(ctx context.Context, email, password string) (bool, error) {
|
||||
var hash string
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
SELECT password_hash FROM admins WHERE email = $1
|
||||
`, email).Scan(&hash)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"shop/internal/models"
|
||||
)
|
||||
|
||||
type ProductRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewProductRepository(pool *pgxpool.Pool) *ProductRepository {
|
||||
return &ProductRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *ProductRepository) Featured(ctx context.Context, limit int) ([]models.Product, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, name, description, price, image_url, category, created_at
|
||||
FROM products
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $1
|
||||
`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var products []models.Product
|
||||
for rows.Next() {
|
||||
var p models.Product
|
||||
if err := rows.Scan(&p.ID, &p.Name, &p.Description, &p.Price, &p.ImageURL, &p.Category, &p.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
products = append(products, p)
|
||||
}
|
||||
return products, rows.Err()
|
||||
}
|
||||
|
||||
func (r *ProductRepository) Categories(ctx context.Context) ([]models.Category, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT category, COUNT(*) AS cnt
|
||||
FROM products
|
||||
GROUP BY category
|
||||
ORDER BY cnt DESC
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var categories []models.Category
|
||||
for rows.Next() {
|
||||
var c models.Category
|
||||
if err := rows.Scan(&c.Name, &c.Count); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Slug = c.Name
|
||||
categories = append(categories, c)
|
||||
}
|
||||
return categories, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user