-
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 aims to provide the foundation for phone books which relay information about existing endpoints in Asterisk. It may be used directly or through something like an LDAP adaptor to provide phonebook services to hardphones.
- Loading branch information
1 parent
4cbad26
commit f75224d
Showing
7 changed files
with
242 additions
and
2 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 Contact struct { | ||
Service *service.Contact | ||
} | ||
|
||
func (p *Contact) Router() chi.Router { | ||
r := chi.NewRouter() | ||
r.Get("/", p.list) | ||
|
||
return r | ||
} | ||
|
||
// @Summary List all contacts in the system. | ||
// @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.ContactPage | ||
// @Tags contacts | ||
// @Router /contacts [get] | ||
func (p *Contact) 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", 10) | ||
if err != nil || page < 0 || pageSize < 0 { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
res, err := p.Service.Paginate(r.Context(), page, pageSize) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
slog.Error("Failed to list contacts", slog.String("path", r.URL.Path), slog.String("reason", err.Error())) | ||
return | ||
} | ||
|
||
content, err := json.Marshal(res) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
slog.Error("Failed to marshall response", slog.String("path", r.URL.Path), slog.String("reason", err.Error())) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(content) | ||
if err != nil { | ||
slog.Error("Failed to write response", slog.String("path", r.URL.Path), slog.String("reason", err.Error())) | ||
} | ||
} |
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,12 @@ | ||
package model | ||
|
||
type Contact struct { | ||
Name string `json:"name"` | ||
Phone string `json:"phone"` | ||
} | ||
|
||
type ContactPage struct { | ||
Total int64 `json:"total"` | ||
Retrieved int `json:"retrieved"` | ||
Contacts []Contact `json:"contacts"` | ||
} |
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,45 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/crazybolillo/eryth/internal/model" | ||
"github.com/crazybolillo/eryth/internal/sqlc" | ||
"github.com/jackc/pgx/v5" | ||
) | ||
|
||
type Contact struct { | ||
Cursor | ||
} | ||
|
||
func (c *Contact) Paginate(ctx context.Context, page, size int) (model.ContactPage, error) { | ||
queries := sqlc.New(c.Cursor) | ||
|
||
rows, err := queries.ListContacts(ctx, sqlc.ListContactsParams{ | ||
Limit: int32(size), | ||
Offset: int32(page), | ||
}) | ||
if err != nil && !errors.Is(err, pgx.ErrNoRows) { | ||
return model.ContactPage{}, err | ||
} | ||
|
||
count, err := queries.CountEndpoints(ctx) | ||
if err != nil { | ||
return model.ContactPage{}, err | ||
} | ||
|
||
contacts := make([]model.Contact, len(rows)) | ||
for idx, row := range rows { | ||
contacts[idx] = model.Contact{ | ||
Name: displayNameFromClid(row.Callerid.String), | ||
Phone: row.Extension.String, | ||
} | ||
} | ||
res := model.ContactPage{ | ||
Total: count, | ||
Retrieved: len(rows), | ||
Contacts: contacts, | ||
} | ||
|
||
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