Skip to content

Commit

Permalink
Fixed smells
Browse files Browse the repository at this point in the history
  • Loading branch information
denisushakov committed Oct 9, 2024
1 parent 0e1b52a commit 31a88d4
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o my_app ./cmd/scheduler/mai
FROM alpine:3.18

ENV TODO_PORT=7540
ENV TODO_DBFILE=/storage/scheduler.db
ENV TODO_DBFILE=/app/storage/scheduler.db

WORKDIR /app

Expand Down
3 changes: 1 addition & 2 deletions cmd/scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

func main() {
cfg := config.MustLoad()
_ = cfg
config.MustLoad()

port := ":" + config.Port

Expand Down
8 changes: 1 addition & 7 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ var (
SecretKeyBytes []byte
)

type Config struct {
}

func MustLoad() *Config {
func MustLoad() {

dir, err := os.Getwd() // current directory
if err != nil {
Expand Down Expand Up @@ -63,9 +60,6 @@ func MustLoad() *Config {
}
SecretKeyBytes = []byte(secretKey)

var cfg Config

return &cfg
}

func absPath(dir, path string) string {
Expand Down
1 change: 1 addition & 0 deletions internal/http-server/handlers/nextdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func GetNextDate(w http.ResponseWriter, r *http.Request) {
if err != nil {
writeErrorResponse(w, err, http.StatusBadRequest)
log.Printf("time cannot pasre: %s", err)
return
}

newDate, err := scheduler.NextDate(nowDate, date, repeat)
Expand Down
2 changes: 1 addition & 1 deletion internal/http-server/handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-chi/chi/v5"
)

func RegisterRoutes(router *chi.Mux, scheduler *scheduler.Scheduler) {
func RegisterRoutes(router *chi.Mux, scheduler *scheduler.Planner) {
router.Get("/api/nextdate", GetNextDate)
router.Post("/api/signin", LoginHandler)

Expand Down
14 changes: 12 additions & 2 deletions internal/http-server/handlers/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ func SaveTask(taskSaver TaskSaver) http.HandlerFunc {

id, err := taskSaver.SaveTask(&task)
if err != nil {
writeErrorResponse(w, err, http.StatusBadRequest)
switch {
case errors.Is(err, sql.ErrConnDone):
writeErrorResponse(w, err, http.StatusInternalServerError)
default:
writeErrorResponse(w, err, http.StatusBadRequest)
}
return
}

Expand All @@ -55,7 +60,12 @@ func GetTasks(taskGetter TaskGetter) http.HandlerFunc {

tasks, err := taskGetter.GetTasks(search)
if err != nil {
writeErrorResponse(w, err, http.StatusBadRequest)
switch {
case errors.Is(err, sql.ErrConnDone):
writeErrorResponse(w, err, http.StatusInternalServerError)
default:
writeErrorResponse(w, err, http.StatusBadRequest)
}
return
}

Expand Down
18 changes: 9 additions & 9 deletions internal/scheduler/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/denisushakov/todo-rest/pkg/models"
)

type Scheduler struct {
type Planner struct {
Storage *sqlite.Storage
}

func NewScheduler(dataBase *sqlite.Storage) *Scheduler {
return &Scheduler{
func NewScheduler(dataBase *sqlite.Storage) *Planner {
return &Planner{
Storage: dataBase,
}
}
Expand All @@ -28,7 +28,7 @@ type TaskScheduler interface {
DeleteTask(string) error
}

func (s *Scheduler) SaveTask(task *models.Task) (int64, error) {
func (s *Planner) SaveTask(task *models.Task) (int64, error) {
if err := check(task); err != nil {
return 0, err
}
Expand All @@ -40,7 +40,7 @@ func (s *Scheduler) SaveTask(task *models.Task) (int64, error) {
return id, nil
}

func (s *Scheduler) GetTasks(search string) ([]*models.Task, error) {
func (s *Planner) GetTasks(search string) ([]*models.Task, error) {

var sr_st sqlite.Search
if search != "" {
Expand All @@ -61,15 +61,15 @@ func (s *Scheduler) GetTasks(search string) ([]*models.Task, error) {
return tasks, nil
}

func (s *Scheduler) GetTaskByID(id string) (*models.Task, error) {
func (s *Planner) GetTaskByID(id string) (*models.Task, error) {
task, err := s.Storage.GetTaskByID(id)
if err != nil {
return nil, err
}
return task, nil
}

func (s *Scheduler) UpdateTask(task *models.Task) error {
func (s *Planner) UpdateTask(task *models.Task) error {
if task.ID == "" {
return fmt.Errorf("id is empty")
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func check(task *models.Task) error {
return nil
}

func (s *Scheduler) MarkTaskCompleted(id string) error {
func (s *Planner) MarkTaskCompleted(id string) error {
var now = time.Now().Truncate(24 * time.Hour)
task, err := s.GetTaskByID(id)
if err != nil {
Expand All @@ -146,7 +146,7 @@ func (s *Scheduler) MarkTaskCompleted(id string) error {
return nil
}

func (s *Scheduler) DeleteTask(id string) error {
func (s *Planner) DeleteTask(id string) error {
if err := s.Storage.DeleteTask(id); err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package router
import (
"log"
"net/http"
"os"

"github.com/denisushakov/todo-rest/internal/scheduler"
"github.com/denisushakov/todo-rest/internal/storage/sqlite"
Expand All @@ -21,17 +20,16 @@ func SetupRouter() *chi.Mux {
storage, err := sqlite.New(config.DBFilePath)
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
os.Exit(1)
}
scheduler := scheduler.NewScheduler(storage)
planner := scheduler.NewScheduler(storage)

router := chi.NewRouter()

router.Use(middleware.URLFormat)

router.Handle("/*", http.FileServer(http.Dir(webDir)))

handlers.RegisterRoutes(router, scheduler)
handlers.RegisterRoutes(router, planner)

return router
}
4 changes: 1 addition & 3 deletions tests/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
)

func createTestServer() *httptest.Server {

cfg := config.MustLoad()
_ = cfg
config.MustLoad()

router := router.SetupRouter()

Expand Down

0 comments on commit 31a88d4

Please sign in to comment.