Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package server

import (
"context"
features "directory/internal/services/api/features"
listings "directory/internal/services/api/listings"

"fmt"
"os"
"os/signal"
Expand All @@ -12,7 +15,6 @@ import (

"directory/internal/router"
"directory/internal/services"
"directory/internal/services/api"
"directory/internal/store/database"
"directory/pkg/config"
db "directory/pkg/database"
Expand Down Expand Up @@ -53,10 +55,16 @@ func initialize(cfg *config.Config) (s *server.Server, err error) {
db, err := db.Connect(cfg)

var services []services.Service
// Stores
featureStore := database.NewFeatureStore(db)
listingStore := database.NewListingStore(db)
contactStore := database.NewContactsStore(db)

// Services
featuresService := features.NewFeatureService(*featureStore, *listingStore)
listingService := listings.NewListingService(*listingStore, *contactStore)

divisionStore := database.NewDivisionStore(db)
divisionService := api.NewDivisionService(*divisionStore)
services = append(services, divisionService)
services = append(services, featuresService, listingService)

var middlewares chi.Middlewares
middlewares = append(middlewares, middleware.Logger)
Expand Down
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SERVER_ADDRESS=:6000

DATABASE_DRIVER=postgres
DATABASE_SOURCE=
DATABASE_SOURCE=postgres://<USER>:<PASSWORD>@<HOST>:5432/directory?search_path=directory
DATABASE_MAX_CONN_LIFETIME=1h
DATABASE_MAX_CONNECTIONS=20
DATABASE_CONNECT_TIMEOUT=5s
Expand Down
180 changes: 0 additions & 180 deletions internal/services/api/divisions.go

This file was deleted.

96 changes: 96 additions & 0 deletions internal/services/api/features/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package api

import (
"directory/pkg/types"
"encoding/json"
"github.com/go-chi/chi/v5"
"net/http"

"directory/internal/store/database"
)

type FeatureService struct {
featureStore database.FeatureStore
listingStore database.ListingStore
}

func NewFeatureService(featuresStore database.FeatureStore, listingStore database.ListingStore) *FeatureService {
return &FeatureService{
featureStore: featuresStore,
listingStore: listingStore,
}
}

func (s *FeatureService) RegisterRoutes(mux *chi.Mux) {
mux.Get("/features/{id}", s.FindByID)
mux.Get("/features/{featureId}/listings", s.FindListingsByFeatureId)

}

func (s *FeatureService) FindByID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

id := chi.URLParam(r, "id")

features, err := s.featureStore.FindByID(ctx, id)
if err != nil {
http.Error(w, "feature id was not found", http.StatusNotFound)
}

response, err := json.Marshal(features)
if err != nil {
http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
_, err = w.Write(response)
if err != nil {
http.Error(w, "Error writing response", http.StatusInternalServerError)
return
}
}

func (s *FeatureService) FindListingsByFeatureId(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

featureId := chi.URLParam(r, "featureId")

featureTree, featureInternalIds, err := s.featureStore.FindRelationsByID(ctx, featureId)
if err != nil {
http.Error(w, "feature id was not found", http.StatusNotFound)
}
listings, err := s.listingStore.FindByListingFeatureInternalIDs(ctx, featureInternalIds)
if err != nil {
http.Error(w, "listings for feature ids were not found", http.StatusNotFound)
}

listingMap := listingsToMap(listings)
for _, feature := range featureTree {
listing, match := listingMap[feature.InternalId]
if match {
feature.Listings = append(feature.Listings, listing)
}
}

response, err := json.Marshal(featureTree)
if err != nil {
http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
_, err = w.Write(response)
if err != nil {
http.Error(w, "Error writing response", http.StatusInternalServerError)
return
}
}

func listingsToMap(listings []*types.Listing) map[int]*types.Listing {
i := map[int]*types.Listing{}
for _, listing := range listings {
i[listing.FeatureId] = listing
}
return i
}
48 changes: 48 additions & 0 deletions internal/services/api/listings/listings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package api

import (
"directory/internal/store/database"
"encoding/json"
"github.com/go-chi/chi/v5"
"net/http"
)

type ListingService struct {
listingStore database.ListingStore
contactStore database.ContactsStore
}

func NewListingService(listingStore database.ListingStore, contactStore database.ContactsStore) *ListingService {
return &ListingService{
contactStore: contactStore,
listingStore: listingStore,
}
}

func (s *ListingService) RegisterRoutes(mux *chi.Mux) {
mux.Get("/listing/{id}/contacts", s.FindContactByListingID)
}

func (s *ListingService) FindContactByListingID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

id := chi.URLParam(r, "id")

contacts, err := s.contactStore.FindContactsByListingIDs(ctx, id)
if err != nil {
http.Error(w, "contacts were not found for the requested listing", http.StatusNotFound)
}

response, err := json.Marshal(contacts)
if err != nil {
http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
_, err = w.Write(response)
if err != nil {
http.Error(w, "Error writing response", http.StatusInternalServerError)
return
}
}
Loading