receipt_indexer/backend/cmd/api/receipts.go
Ethan Wellenreiter a2837b6d82 Beginning to work on the functions used in api.go (the handlers)
Mostly just function signatures at the moment

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2025-04-30 21:03:20 -04:00

71 lines
1.9 KiB
Go

package main
import (
"context"
"net/http"
"git.ewellenr.ca/receipt_indexer/backend/internal/storage"
)
type receiptKey string
const receiptCtx receiptKey = "receipt"
func (app *application) getReceiptsHandler(w http.ResponseWriter, r *http.Request) {
// get the page and size from context
// default them to something
}
func (app *application) getReceiptHandler(w http.ResponseWriter, r *http.Request) {
}
func (app *application) createReceiptHandler(w http.ResponseWriter, r *http.Request) {
// handle receipt creation logic here
}
func (app *application) updateReceiptHandler(w http.ResponseWriter, r *http.Request) {
// handle receipt update logic here
// Not too sure what to do here. need to break it down into what can actually be updated via the api
}
func (app *application) deleteReceiptHandler(w http.ResponseWriter, r *http.Request) {
// delete the receipt
// should be as simple as getting the receipt and then calling the delete function.
// Should also delete from cache if cache is enabled
// the delete function should be like a transaction. It should do the transaction for the receipt and images in the db.
// Then it should try and delete all the images in the S3/minio bucket. Abort/commit otherwise
}
func (app *application) getReceipt(ctx context.Context, receiptID int64) (*storage.Receipt, error) {
if !app.config.redisCfg.enabled {
return app.store.Receipts.GetByID(ctx, receiptID)
}
receipt, err := app.cacheStorage.Receipts.Get(ctx, receiptID)
if err != nil {
return nil, err
}
if receipt == nil {
receipt, err = app.store.Receipts.GetByID(ctx, receiptID)
if err != nil {
return nil, err
}
if err := app.cacheStorage.Receipts.Set(ctx, receipt); err != nil {
return nil, err
}
}
return receipt, nil
}
func getReceiptFromContext(r *http.Request) *storage.Receipt {
receipt, _ := r.Context().Value(receiptCtx).(*storage.Receipt)
return receipt
}