From 7623abb01db89955e5b833324004cda302dad9bc Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Sun, 1 Mar 2026 19:48:44 +0100 Subject: [PATCH] sw api endpoint --- domain/service.software.go | 23 +++++++++++++++++++++++ http/controller.api.software.go | 21 +++++++++++++++++++++ http/http.go | 1 + http/router.go | 6 ++++++ 4 files changed, 51 insertions(+) create mode 100644 http/controller.api.software.go diff --git a/domain/service.software.go b/domain/service.software.go index 7257a35..8f87e5b 100644 --- a/domain/service.software.go +++ b/domain/service.software.go @@ -10,6 +10,10 @@ type SoftwareListDTO struct { LatestRelease *Release } +type SoftwareDetaildListDTO struct { + Softwares []SoftwareShowData +} + type SoftwareShowData struct { Software *Software Releases []Release @@ -19,6 +23,7 @@ type SoftwareShowData struct { type SoftwareServiceInterface interface { List() ([]SoftwareListDTO, error) + DetailedList() (*SoftwareDetaildListDTO, error) GetByName(name string) (*Software, error) GetLatestRelease(software_id string) (*Release, error) GetForShowByName(name string) (*SoftwareShowData, error) @@ -116,3 +121,21 @@ func (s *SoftwareService) GetLatestRelease(software_id string) (*Release, error) }) return &software.Releases[0], nil } + +func (s *SoftwareService) DetailedList() (*SoftwareDetaildListDTO, error) { + softwares, err := s.softeare_repository.List() + if err != nil { + return nil, err + } + + dtos := make([]SoftwareShowData, 0, len(softwares)) + for _, software := range softwares { + showData, err := s.GetForShowByName(software.Name) + if err != nil { + return nil, err + } + dtos = append(dtos, *showData) + } + + return &SoftwareDetaildListDTO{Softwares: dtos}, nil +} diff --git a/http/controller.api.software.go b/http/controller.api.software.go new file mode 100644 index 0000000..638e6bd --- /dev/null +++ b/http/controller.api.software.go @@ -0,0 +1,21 @@ +package http + +import ( + "encoding/json" + "net/http" + + "teletype_softwares/domain" +) + +type APISoftwareController struct { + softwareService domain.SoftwareServiceInterface +} + +func NewAPISoftwareController(software_service domain.SoftwareServiceInterface) *APISoftwareController { + return &APISoftwareController{softwareService: software_service} +} + +func (c *APISoftwareController) Index(w http.ResponseWriter, r *http.Request) { + softwares, _ := c.softwareService.DetailedList() + json.NewEncoder(w).Encode(softwares) +} diff --git a/http/http.go b/http/http.go index e9affb4..9429a23 100755 --- a/http/http.go +++ b/http/http.go @@ -7,6 +7,7 @@ import ( func StartHttpServer(domain_instance domain.Domain) { router := NewRouter( + NewAPISoftwareController(domain_instance.SoftwareService), NewSoftwareController(domain_instance.SoftwareService), NewSoftwareUpdaterController(domain_instance.SoftwareUpdaterService, domain_instance.SoftwareService), NewDownloadController(domain_instance.DownloadService, domain_instance.SoftwareService), diff --git a/http/router.go b/http/router.go index 68e11f7..e19f86b 100755 --- a/http/router.go +++ b/http/router.go @@ -7,6 +7,7 @@ import ( ) type Router struct { + apiSoftwareController *APISoftwareController softwareController *SoftwareController softwareUpdaterController *SoftwareUpdaterController downloadController *DownloadController @@ -16,6 +17,7 @@ type Router struct { } func NewRouter( + api_software_controller *APISoftwareController, software_controller *SoftwareController, software_updater_controller *SoftwareUpdaterController, download_controller *DownloadController, @@ -24,6 +26,7 @@ func NewRouter( docs_controller *DocsController, ) *Router { return &Router{ + apiSoftwareController: api_software_controller, softwareController: software_controller, softwareUpdaterController: software_updater_controller, downloadController: download_controller, @@ -38,6 +41,9 @@ func (r *Router) Init() *chi.Mux { router.Use(CORSMiddleware) router.Get("/", r.rootController.Index) + + router.Get("/api/software", r.apiSoftwareController.Index) + router.Get("/software", r.softwareController.Index) router.Get("/software/{name}", r.softwareController.Show)