feat: implement mongo APIs
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ func (h *LetterHandler) GetByYear(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
docs, err := h.repo.GetByYear(r.Context(), year)
|
docs, err := h.repo.GetByYear(r.Context(), year)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] GetByYear(%d): %v", year, err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to fetch letters")
|
response.Error(w, http.StatusInternalServerError, "failed to fetch letters")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -50,6 +52,7 @@ func (h *LetterHandler) GetByRecipient(w http.ResponseWriter, r *http.Request) {
|
|||||||
name := chi.URLParam(r, "name")
|
name := chi.URLParam(r, "name")
|
||||||
docs, err := h.repo.GetByRecipient(r.Context(), name)
|
docs, err := h.repo.GetByRecipient(r.Context(), name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] GetByRecipient(%s): %v", name, err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to fetch letters")
|
response.Error(w, http.StatusInternalServerError, "failed to fetch letters")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -60,6 +63,7 @@ func (h *LetterHandler) GetByLocation(w http.ResponseWriter, r *http.Request) {
|
|||||||
name := chi.URLParam(r, "name")
|
name := chi.URLParam(r, "name")
|
||||||
docs, err := h.repo.GetByLocation(r.Context(), name)
|
docs, err := h.repo.GetByLocation(r.Context(), name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] GetByLocation(%s): %v", name, err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to fetch letters")
|
response.Error(w, http.StatusInternalServerError, "failed to fetch letters")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -69,6 +73,7 @@ func (h *LetterHandler) GetByLocation(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (h *LetterHandler) GetYears(w http.ResponseWriter, r *http.Request) {
|
func (h *LetterHandler) GetYears(w http.ResponseWriter, r *http.Request) {
|
||||||
docs, err := h.repo.GetYears(r.Context())
|
docs, err := h.repo.GetYears(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] GetYears: %v", err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to fetch years")
|
response.Error(w, http.StatusInternalServerError, "failed to fetch years")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -78,6 +83,7 @@ func (h *LetterHandler) GetYears(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (h *LetterHandler) GetRecipients(w http.ResponseWriter, r *http.Request) {
|
func (h *LetterHandler) GetRecipients(w http.ResponseWriter, r *http.Request) {
|
||||||
docs, err := h.repo.GetRecipients(r.Context())
|
docs, err := h.repo.GetRecipients(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] GetRecipients: %v", err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to fetch recipients")
|
response.Error(w, http.StatusInternalServerError, "failed to fetch recipients")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -87,6 +93,7 @@ func (h *LetterHandler) GetRecipients(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (h *LetterHandler) GetLocations(w http.ResponseWriter, r *http.Request) {
|
func (h *LetterHandler) GetLocations(w http.ResponseWriter, r *http.Request) {
|
||||||
docs, err := h.repo.GetLocations(r.Context())
|
docs, err := h.repo.GetLocations(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] GetLocations: %v", err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to fetch locations")
|
response.Error(w, http.StatusInternalServerError, "failed to fetch locations")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -96,6 +103,7 @@ func (h *LetterHandler) GetLocations(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (h *LetterHandler) Count(w http.ResponseWriter, r *http.Request) {
|
func (h *LetterHandler) Count(w http.ResponseWriter, r *http.Request) {
|
||||||
count, err := h.repo.Count(r.Context())
|
count, err := h.repo.Count(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] Count: %v", err)
|
||||||
response.Error(w, http.StatusInternalServerError, "failed to count letters")
|
response.Error(w, http.StatusInternalServerError, "failed to count letters")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ type EssaySummaryDoc struct {
|
|||||||
|
|
||||||
func (r *EssayRepo) GetAll(ctx context.Context) ([]EssaySummaryDoc, error) {
|
func (r *EssayRepo) GetAll(ctx context.Context) ([]EssaySummaryDoc, error) {
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"group": 1, "order": 1}).
|
SetSort(bson.D{{Key: "group", Value: 1}, {Key: "order", Value: 1}}).
|
||||||
SetProjection(bson.M{"slug": 1, "title": 1, "year": 1, "group": 1})
|
SetProjection(bson.M{"slug": 1, "title": 1, "year": 1, "group": 1})
|
||||||
cursor, err := r.col.Find(ctx, bson.M{}, opts)
|
cursor, err := r.col.Find(ctx, bson.M{}, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -34,17 +34,18 @@ type LetterDoc struct {
|
|||||||
type LetterSummaryDoc struct {
|
type LetterSummaryDoc struct {
|
||||||
ID bson.ObjectID `bson:"_id" json:"id"`
|
ID bson.ObjectID `bson:"_id" json:"id"`
|
||||||
Slug string `bson:"slug" json:"slug"`
|
Slug string `bson:"slug" json:"slug"`
|
||||||
Recipients []string `bson:"recipients" json:"recipients"`
|
Recipients []string `bson:"recipients,omitempty" json:"recipients"`
|
||||||
RecipientDisplay string `bson:"recipientDisplay" json:"recipient_display"`
|
RecipientDisplay string `bson:"recipientDisplay" json:"recipient_display"`
|
||||||
Date *string `bson:"date,omitempty" json:"date,omitempty"`
|
Date *string `bson:"date,omitempty" json:"date,omitempty"`
|
||||||
Year *int `bson:"year,omitempty" json:"year,omitempty"`
|
Year *int `bson:"year,omitempty" json:"year,omitempty"`
|
||||||
Month *int `bson:"month,omitempty" json:"month,omitempty"`
|
Month *int `bson:"month,omitempty" json:"month,omitempty"`
|
||||||
|
Day *int `bson:"day,omitempty" json:"day,omitempty"`
|
||||||
Location *string `bson:"location,omitempty" json:"location,omitempty"`
|
Location *string `bson:"location,omitempty" json:"location,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var letterSummaryProjection = bson.M{
|
var letterSummaryProjection = bson.M{
|
||||||
"slug": 1, "recipients": 1, "recipientDisplay": 1,
|
"slug": 1, "recipients": 1, "recipientDisplay": 1,
|
||||||
"date": 1, "year": 1, "month": 1, "location": 1,
|
"date": 1, "year": 1, "month": 1, "day": 1, "location": 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LetterRepo) GetBySlug(ctx context.Context, slug string) (*LetterDoc, error) {
|
func (r *LetterRepo) GetBySlug(ctx context.Context, slug string) (*LetterDoc, error) {
|
||||||
@@ -61,7 +62,7 @@ func (r *LetterRepo) GetBySlug(ctx context.Context, slug string) (*LetterDoc, er
|
|||||||
|
|
||||||
func (r *LetterRepo) GetByYear(ctx context.Context, year int) ([]LetterSummaryDoc, error) {
|
func (r *LetterRepo) GetByYear(ctx context.Context, year int) ([]LetterSummaryDoc, error) {
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(letterSummaryProjection)
|
SetProjection(letterSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, bson.M{"year": year}, opts)
|
cursor, err := r.col.Find(ctx, bson.M{"year": year}, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -77,7 +78,7 @@ func (r *LetterRepo) GetByYear(ctx context.Context, year int) ([]LetterSummaryDo
|
|||||||
|
|
||||||
func (r *LetterRepo) GetByRecipient(ctx context.Context, name string) ([]LetterSummaryDoc, error) {
|
func (r *LetterRepo) GetByRecipient(ctx context.Context, name string) ([]LetterSummaryDoc, error) {
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"year": 1, "month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "year", Value: 1}, {Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(letterSummaryProjection)
|
SetProjection(letterSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, bson.M{"recipients": name}, opts)
|
cursor, err := r.col.Find(ctx, bson.M{"recipients": name}, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -93,7 +94,7 @@ func (r *LetterRepo) GetByRecipient(ctx context.Context, name string) ([]LetterS
|
|||||||
|
|
||||||
func (r *LetterRepo) GetByLocation(ctx context.Context, location string) ([]LetterSummaryDoc, error) {
|
func (r *LetterRepo) GetByLocation(ctx context.Context, location string) ([]LetterSummaryDoc, error) {
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"year": 1, "month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "year", Value: 1}, {Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(letterSummaryProjection)
|
SetProjection(letterSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, bson.M{"location": location}, opts)
|
cursor, err := r.col.Find(ctx, bson.M{"location": location}, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ func (r *TranscriptionRepo) GetBySlug(ctx context.Context, slug string) (*Transc
|
|||||||
func (r *TranscriptionRepo) GetByYear(ctx context.Context, year int) ([]TranscriptionSummaryDoc, error) {
|
func (r *TranscriptionRepo) GetByYear(ctx context.Context, year int) ([]TranscriptionSummaryDoc, error) {
|
||||||
filter := bson.M{"year": year, "isPublished": true}
|
filter := bson.M{"year": year, "isPublished": true}
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(transcriptionSummaryProjection)
|
SetProjection(transcriptionSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, filter, opts)
|
cursor, err := r.col.Find(ctx, filter, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,7 +113,7 @@ func (r *TranscriptionRepo) GetByYear(ctx context.Context, year int) ([]Transcri
|
|||||||
func (r *TranscriptionRepo) GetByPlace(ctx context.Context, place string) ([]TranscriptionSummaryDoc, error) {
|
func (r *TranscriptionRepo) GetByPlace(ctx context.Context, place string) ([]TranscriptionSummaryDoc, error) {
|
||||||
filter := bson.M{"place": place, "isPublished": true}
|
filter := bson.M{"place": place, "isPublished": true}
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"year": 1, "month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "year", Value: 1}, {Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(transcriptionSummaryProjection)
|
SetProjection(transcriptionSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, filter, opts)
|
cursor, err := r.col.Find(ctx, filter, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -130,7 +130,7 @@ func (r *TranscriptionRepo) GetByPlace(ctx context.Context, place string) ([]Tra
|
|||||||
func (r *TranscriptionRepo) GetByType(ctx context.Context, docType string) ([]TranscriptionSummaryDoc, error) {
|
func (r *TranscriptionRepo) GetByType(ctx context.Context, docType string) ([]TranscriptionSummaryDoc, error) {
|
||||||
filter := bson.M{"type": docType, "isPublished": true}
|
filter := bson.M{"type": docType, "isPublished": true}
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"year": 1, "month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "year", Value: 1}, {Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(transcriptionSummaryProjection)
|
SetProjection(transcriptionSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, filter, opts)
|
cursor, err := r.col.Find(ctx, filter, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -199,7 +199,7 @@ func (r *TranscriptionRepo) Count(ctx context.Context) (int64, error) {
|
|||||||
func (r *TranscriptionRepo) GetAdjacent(ctx context.Context, slug string, year int) (*TranscriptionSummaryDoc, *TranscriptionSummaryDoc, error) {
|
func (r *TranscriptionRepo) GetAdjacent(ctx context.Context, slug string, year int) (*TranscriptionSummaryDoc, *TranscriptionSummaryDoc, error) {
|
||||||
filter := bson.M{"year": year, "isPublished": true}
|
filter := bson.M{"year": year, "isPublished": true}
|
||||||
opts := options.Find().
|
opts := options.Find().
|
||||||
SetSort(bson.M{"month": 1, "day": 1}).
|
SetSort(bson.D{{Key: "month", Value: 1}, {Key: "day", Value: 1}}).
|
||||||
SetProjection(transcriptionSummaryProjection)
|
SetProjection(transcriptionSummaryProjection)
|
||||||
cursor, err := r.col.Find(ctx, filter, opts)
|
cursor, err := r.col.Find(ctx, filter, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user