From d8b3dc7b159da006c8222e025c3ba353a5efd078 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 1 Oct 2023 13:17:10 +0900 Subject: [PATCH] /v1/accounts/{account_id} --- api/v1/accounts/account_id/handler.go | 78 +++++++++++++++++++++++++++ api/v1/accounts/account_id/loader.go | 8 +++ 2 files changed, 86 insertions(+) create mode 100644 api/v1/accounts/account_id/handler.go create mode 100644 api/v1/accounts/account_id/loader.go diff --git a/api/v1/accounts/account_id/handler.go b/api/v1/accounts/account_id/handler.go new file mode 100644 index 0000000..ff4ebf3 --- /dev/null +++ b/api/v1/accounts/account_id/handler.go @@ -0,0 +1,78 @@ +package account_id + +import ( + "net/http" + + "sourcecode.social/reiver/go-mstdn" + "sourcecode.social/reiver/go-mstdn/ent" +) + +var _ http.Handler = Handler{} + +const Path string = "/api/v1/accounts/{account_id}" + +type Handler struct { + LoaderFunc LoaderFunc +} + +func (Handler) Path() string { + return Path +} + +func (receiver Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + if nil == resp { + return + } + + if nil == req { + mstdn.InternalServerError(resp) + return + } + + if http.MethodGet != req.Method { + mstdn.MethodNotAllowed(resp) + return + } + + fn := receiver.LoaderFunc + if nil == fn { + mstdn.InternalServerError(resp) + return + } + + var accountid string + { + const key string = "accountid" + + url := req.URL + if nil == url { + mstdn.InternalServerError(resp) + return + } + + query := url.Query() + if nil == query { + mstdn.InternalServerError(resp) + return + } + + if !query.Has(key) { + mstdn.BadRequest(resp) + return + } + + accountid = query.Get("accountid") + } + + var account ent.Account + { + + err := fn(&account, accountid) + if nil != err { + mstdn.Error(resp, err.ErrHTTP()) + return + } + } + + mstdn.OK(resp, account) +} diff --git a/api/v1/accounts/account_id/loader.go b/api/v1/accounts/account_id/loader.go new file mode 100644 index 0000000..4d3f8eb --- /dev/null +++ b/api/v1/accounts/account_id/loader.go @@ -0,0 +1,8 @@ +package account_id + +import ( + "sourcecode.social/reiver/go-mstdn/api/v1" + "sourcecode.social/reiver/go-mstdn/ent" +) + +type LoaderFunc func(account *ent.Account, accountid string) v1.Error