-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This endpoint makes it possible to paginate through call detail records.
- Loading branch information
1 parent
65d84fc
commit ef7f573
Showing
7 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/crazybolillo/eryth/internal/query" | ||
"github.com/crazybolillo/eryth/internal/service" | ||
"github.com/go-chi/chi/v5" | ||
"log/slog" | ||
"net/http" | ||
) | ||
|
||
type Cdr struct { | ||
Service *service.Cdr | ||
} | ||
|
||
func (c *Cdr) Router() chi.Router { | ||
r := chi.NewRouter() | ||
r.Get("/", c.list) | ||
|
||
return r | ||
} | ||
|
||
// @Summary List call detail records. | ||
// @Param page query int false "Zero based page to fetch" default(0) | ||
// @Param pageSize query int false "Max amount of results to be returned" default(20) | ||
// @Produce json | ||
// @Success 200 {object} model.CallRecordPage | ||
// @Tags cdr | ||
// @Router /cdr [get] | ||
func (c *Cdr) list(w http.ResponseWriter, r *http.Request) { | ||
page, err := query.GetIntOr(r.URL.Query(), "page", 0) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
pageSize, err := query.GetIntOr(r.URL.Query(), "pageSize", 25) | ||
if err != nil || page < 0 || pageSize < 0 { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
res, err := c.Service.Paginate(r.Context(), page, pageSize) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
slog.Error("Failed to list cdr", "path", r.URL.Path, "reason", err) | ||
return | ||
} | ||
|
||
content, err := json.Marshal(res) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
slog.Error("Failed to marshall response", "path", r.URL.Path, "reason", err) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(content) | ||
if err != nil { | ||
slog.Error("Failed to write response", "path", r.URL.Path, "reason", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/crazybolillo/eryth/internal/sqlc" | ||
"github.com/crazybolillo/eryth/pkg/model" | ||
"github.com/jackc/pgx/v5" | ||
"time" | ||
) | ||
|
||
type Cdr struct { | ||
Cursor Cursor | ||
} | ||
|
||
func (c *Cdr) Paginate(ctx context.Context, page, size int) (model.CallRecordPage, error) { | ||
queries := sqlc.New(c.Cursor) | ||
|
||
rows, err := queries.ListCallRecords(ctx, sqlc.ListCallRecordsParams{ | ||
Limit: int32(size), | ||
Offset: int32(page), | ||
}) | ||
if err != nil && !errors.Is(err, pgx.ErrNoRows) { | ||
return model.CallRecordPage{}, err | ||
} | ||
|
||
count, err := queries.CountCallRecords(ctx) | ||
if err != nil { | ||
return model.CallRecordPage{}, err | ||
} | ||
|
||
records := make([]model.CallRecord, len(rows)) | ||
for idx, row := range rows { | ||
var answer *time.Time | ||
answerVal, ok := row.Answer.(time.Time) | ||
if !ok { | ||
answer = nil | ||
} else { | ||
answer = &answerVal | ||
} | ||
|
||
records[idx] = model.CallRecord{ | ||
ID: row.ID, | ||
From: row.Origin.String, | ||
To: row.Destination.String, | ||
Context: row.Context.String, | ||
Start: row.CallStart.Time, | ||
Answer: answer, | ||
End: row.CallEnd.Time, | ||
Duration: row.Duration.Int32, | ||
BillSeconds: row.Billsec.Int32, | ||
} | ||
} | ||
|
||
res := model.CallRecordPage{ | ||
Records: records, | ||
Total: count, | ||
Retrieved: len(records), | ||
} | ||
|
||
return res, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package model | ||
|
||
import "time" | ||
|
||
type CallRecord struct { | ||
ID int64 `json:"id"` | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
Context string `json:"context"` | ||
Start time.Time `json:"start"` | ||
Answer *time.Time `json:"answer"` | ||
End time.Time `json:"end"` | ||
Duration int32 `json:"duration"` | ||
BillSeconds int32 `json:"billsec"` | ||
} | ||
|
||
type CallRecordPage struct { | ||
Total int64 `json:"total"` | ||
Retrieved int `json:"retrieved"` | ||
Records []CallRecord `json:"records"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters