From 645c8966c4ff34d845efbc1e8da1c27d46cfb4c5 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Tue, 27 Jan 2026 21:31:47 +0100 Subject: [PATCH] sw show page --- domain/service.software.go | 32 ++++++++++--- http/controller.play.go | 2 +- http/controller.software.go | 46 ++++++++++++------- http/router.go | 5 +- http/views/software/index.html | 4 +- .../software/{releases.html => show.html} | 4 +- 6 files changed, 62 insertions(+), 31 deletions(-) rename http/views/software/{releases.html => show.html} (85%) diff --git a/domain/service.software.go b/domain/service.software.go index a7e72e7..4f80512 100644 --- a/domain/service.software.go +++ b/domain/service.software.go @@ -9,19 +9,20 @@ type SoftwareListDTO struct { type SoftwareServiceInterface interface { List() ([]SoftwareListDTO, error) - GetByNameWithReleases(name string) (*Software, error) + GetByName(name string) (*Software, error) + GetLatestRelease(softwareID string) (*Release, error) } type SoftwareService struct { - repository SoftwareRepositoryInterface + softeare_repository SoftwareRepositoryInterface } -func NewSoftwareService(repository SoftwareRepositoryInterface) *SoftwareService { - return &SoftwareService{repository: repository} +func NewSoftwareService(softeare_repository SoftwareRepositoryInterface) *SoftwareService { + return &SoftwareService{softeare_repository: softeare_repository} } func (s *SoftwareService) List() ([]SoftwareListDTO, error) { - softwares, err := s.repository.List() + softwares, err := s.softeare_repository.List() if err != nil { return nil, err } @@ -41,6 +42,23 @@ func (s *SoftwareService) List() ([]SoftwareListDTO, error) { return dtos, nil } -func (s *SoftwareService) GetByNameWithReleases(name string) (*Software, error) { - return s.repository.GetByName(name) +func (s *SoftwareService) GetByName(name string) (*Software, error) { + return s.softeare_repository.GetByName(name) +} + +func (s *SoftwareService) GetLatestRelease(softwareID string) (*Release, error) { + software, err := s.softeare_repository.GetByName(softwareID) + if err != nil { + return nil, err + } + if software == nil { + return nil, nil + } + if len(software.Releases) == 0 { + return nil, nil + } + sort.Slice(software.Releases, func(i, j int) bool { + return software.Releases[i].CreatedAt.After(software.Releases[j].CreatedAt) + }) + return &software.Releases[0], nil } diff --git a/http/controller.play.go b/http/controller.play.go index 12d2a30..7b76d47 100644 --- a/http/controller.play.go +++ b/http/controller.play.go @@ -26,7 +26,7 @@ func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name") version := chi.URLParam(r, "version") - software, err := c.softwareService.GetByNameWithReleases(name) + software, err := c.softwareService.GetByName(name) if err != nil { if err == domain.ErrSoftwareNotFound { http.Error(w, "Software not found", http.StatusNotFound) diff --git a/http/controller.software.go b/http/controller.software.go index 6cdf85c..4559761 100644 --- a/http/controller.software.go +++ b/http/controller.software.go @@ -10,15 +10,15 @@ import ( ) type SoftwareController struct { - service domain.SoftwareServiceInterface + softwareService domain.SoftwareServiceInterface } -func NewSoftwareController(service domain.SoftwareServiceInterface) *SoftwareController { - return &SoftwareController{service: service} +func NewSoftwareController(software_service domain.SoftwareServiceInterface) *SoftwareController { + return &SoftwareController{softwareService: software_service} } func (c *SoftwareController) index(w http.ResponseWriter, r *http.Request) { - softwares, err := c.service.List() + softwares, err := c.softwareService.List() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -33,25 +33,37 @@ func (c *SoftwareController) index(w http.ResponseWriter, r *http.Request) { tmpl.Execute(w, softwares) } -func (c *SoftwareController) releases(w http.ResponseWriter, r *http.Request) { +type SoftwareShowData struct { + Software *domain.Software + LatestVersion string +} + +func (c *SoftwareController) show(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name") - software, err := c.service.GetByNameWithReleases(name) - if err != nil { - if err == domain.ErrSoftwareNotFound { - http.Error(w, "Software not found", http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - tmpl, err := template_utils.GetTemplate("software_releases", "http/views/shared/layout.html", "http/views/software/releases.html") + software, err := c.softwareService.GetByName(name) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + if software == nil { + http.NotFound(w, r) + return + } - tmpl.Execute(w, map[string]interface{}{ - "Software": software, + latest_release, err := c.softwareService.GetLatestRelease(software.Name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + if latest_release == nil { + http.NotFound(w, r) + return + } + + tmpl, _ := template_utils.GetTemplate("software_show", "http/views/shared/layout.html", "http/views/software/show.html") + tmpl.Execute(w, SoftwareShowData{ + Software: software, + LatestVersion: latest_release.Version, }) } diff --git a/http/router.go b/http/router.go index 0a1cf8b..abf9e34 100755 --- a/http/router.go +++ b/http/router.go @@ -34,9 +34,10 @@ func (r *Router) Init() *chi.Mux { router := chi.NewRouter() router.Get("/", r.rootController.index) - router.Get("/softwares", r.softwareController.index) + router.Get("/software", r.softwareController.index) + router.Get("/software/{name}", r.softwareController.show) + router.Get("/update", r.softwareUpdaterController.update) - router.Get("/releases/{name}", r.softwareController.releases) router.Get("/download/{name}/source", r.downloadController.GetLatestSource) router.Get("/download/{name}/cartridge", r.downloadController.GetLatestCartridge) router.Get("/download/{name}/{version}/source", r.downloadController.GetSource) diff --git a/http/views/software/index.html b/http/views/software/index.html index 71b451e..4af448a 100644 --- a/http/views/software/index.html +++ b/http/views/software/index.html @@ -17,8 +17,8 @@
Source Cartridge - Releases - Play + Releases + Play
diff --git a/http/views/software/releases.html b/http/views/software/show.html similarity index 85% rename from http/views/software/releases.html rename to http/views/software/show.html index d0aff97..723a047 100644 --- a/http/views/software/releases.html +++ b/http/views/software/show.html @@ -7,11 +7,11 @@
Version: {{.Version}}
-
Released At: {{.CreatedAt.Format "2006-01-02 15:04:05"}}
+

Released At: {{.CreatedAt.Format "2006-01-02 15:04:05"}}