Skip to content

Commit

Permalink
place endpoint are added
Browse files Browse the repository at this point in the history
  • Loading branch information
timurIsaevIY committed Sep 28, 2024
1 parent 71660cc commit 0a796ee
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"TripAdvisor/pkg/auth"
"encoding/json"
"flag"
"fmt"
"log"
Expand All @@ -19,6 +21,8 @@ type application struct {
}

func main() {
repos := auth.NewRepository()
repoUsecase := auth.NewRepoUsecase(repos)

Check failure on line 25 in cmd/main.go

View workflow job for this annotation

GitHub Actions / linters

repoUsecase declared and not used
var cfg config
flag.IntVar(&cfg.port, "port", 8080, "API server port")
flag.StringVar(&cfg.env, "env", "development", "Environment")
Expand All @@ -30,6 +34,7 @@ func main() {
}
mux := http.NewServeMux()
mux.HandleFunc("/healthcheck", app.healthcheckHandler)
mux.HandleFunc("/place", app.getPlaceHandler)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.port),
Handler: mux,
Expand All @@ -50,3 +55,16 @@ func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Reques
fmt.Printf("ERROR: healthcheckHandler: %s\n", err)
}
}

func (app *application) getPlaceHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
places, err := repoUsecase.getPlaces()

Check failure on line 61 in cmd/main.go

View workflow job for this annotation

GitHub Actions / linters

undefined: repoUsecase (typecheck)
if err != nil {
http.Error(w, "Не удалось получить список достопримечательностей", http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(places)
if err != nil {
return
}
}
37 changes: 37 additions & 0 deletions pkg/auth/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package auth

import "database/sql"

type Repo interface {
getPlaces() ([]Place, error)
}

type Repository struct{}

func NewRepository() *Repository {
return &Repository{}
}

func (r *Repository) getPlaces() ([]Place, error) {
connStr := "user=postgres password=mypassword host=localhost port=5432 dbname=landmarks sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
defer db.Close()
rows, err := db.Query("SELECT * FROM places")
if err != nil {
return nil, err
}
defer rows.Close()
places := []Place{}
for rows.Next() {
var place Place
err := rows.Scan(&place.ID, &place.Name, &place.Image)
if err != nil {
return nil, err
}
places = append(places, place)
}
return places, nil
}
27 changes: 27 additions & 0 deletions pkg/auth/usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package auth

type Place struct {
ID int
Name string
Image string
}

type PlaceUsecase interface {
getPlace() ([]Place, error)
}

type RepoUsecase struct {
repos Repository
}

func NewRepoUsecase(repos *Repository) *RepoUsecase {
return &RepoUsecase{repos: *repos}
}

func (i *RepoUsecase) getPlace() ([]Place, error) {
places, err := i.repos.getPlaces()
if err != nil {
return nil, err
}
return places, nil
}

0 comments on commit 0a796ee

Please sign in to comment.