initial commit
This commit is contained in:
parent
9baa0fab25
commit
fc46164556
14 changed files with 243 additions and 0 deletions
19
internal/app/routes.go
Normal file
19
internal/app/routes.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"maot-shortner/internal/handlers"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HandlerList struct {
|
||||
*handlers.LinkHandler
|
||||
}
|
||||
|
||||
func CreateMux(handlers HandlerList) *http.ServeMux {
|
||||
r := http.NewServeMux()
|
||||
|
||||
r.HandleFunc("/links", handlers.LinkHandler.ListLinks)
|
||||
r.HandleFunc("/", handlers.LinkHandler.Handle)
|
||||
|
||||
return r
|
||||
}
|
||||
1
internal/config/config.go
Normal file
1
internal/config/config.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package config
|
||||
23
internal/database/database.go
Normal file
23
internal/database/database.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"log"
|
||||
"maot-shortner/internal/domain"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func InitDatabase() (database *Database, err error) {
|
||||
log.Println("Starting database...")
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("shortener.db"), &gorm.Config{})
|
||||
db.AutoMigrate(domain.ImportModels()...)
|
||||
|
||||
database = &Database{db}
|
||||
return database, err
|
||||
}
|
||||
16
internal/database/link.go
Normal file
16
internal/database/link.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maot-shortner/internal/domain"
|
||||
)
|
||||
|
||||
func (db *Database) GetLinks(ctx context.Context) ([]*domain.Link, error) {
|
||||
links := make([]*domain.Link, 0)
|
||||
|
||||
if err := db.WithContext(ctx).Find(&links).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return links, nil
|
||||
}
|
||||
12
internal/domain/link.go
Normal file
12
internal/domain/link.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
ID int64 `json:"id"`
|
||||
ShortURL string `json:"short_url"`
|
||||
LongURL string `json:"long_url"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
7
internal/domain/models.go
Normal file
7
internal/domain/models.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package domain
|
||||
|
||||
func ImportModels() []any {
|
||||
return []any{
|
||||
&Link{},
|
||||
}
|
||||
}
|
||||
7
internal/handlers/handler.go
Normal file
7
internal/handlers/handler.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package handlers
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Handler interface {
|
||||
Handle(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
51
internal/handlers/link.go
Normal file
51
internal/handlers/link.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maot-shortner/internal/database"
|
||||
"maot-shortner/internal/services"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type LinkHandler struct {
|
||||
db *database.Database
|
||||
linkService *services.LinkService
|
||||
}
|
||||
|
||||
func (h *LinkHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
json.NewEncoder(w).Encode(&Response{Message: "Ola"})
|
||||
}
|
||||
|
||||
func NewLinkHandler(db *database.Database) *LinkHandler {
|
||||
return &LinkHandler{
|
||||
db: db,
|
||||
linkService: services.NewLinkService(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *LinkHandler) ListLinks(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
linkList, err := h.linkService.GetLinks(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to list links: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(linkList)
|
||||
}
|
||||
|
||||
func (h *LinkHandler) GetUrl(w http.ResponseWriter, r *http.Request) {
|
||||
currentCode := r.PathValue("id")
|
||||
|
||||
json.NewEncoder(w).Encode(&Response{Message: currentCode})
|
||||
}
|
||||
10
internal/repositories/link.go
Normal file
10
internal/repositories/link.go
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maot-shortner/internal/domain"
|
||||
)
|
||||
|
||||
type LinkRepository interface {
|
||||
GetLinks(ctx context.Context) ([]*domain.Link, error)
|
||||
}
|
||||
21
internal/services/link.go
Normal file
21
internal/services/link.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maot-shortner/internal/domain"
|
||||
"maot-shortner/internal/repositories"
|
||||
)
|
||||
|
||||
type LinkService struct {
|
||||
repo repositories.LinkRepository
|
||||
}
|
||||
|
||||
func NewLinkService(repo repositories.LinkRepository) *LinkService {
|
||||
return &LinkService{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LinkService) GetLinks(ctx context.Context) ([]*domain.Link, error) {
|
||||
return s.repo.GetLinks(ctx)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue