sw show page

This commit is contained in:
2026-01-27 21:31:47 +01:00
parent cb4fdb4071
commit 645c8966c4
6 changed files with 62 additions and 31 deletions
+25 -7
View File
@@ -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
}
+1 -1
View File
@@ -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)
+29 -17
View File
@@ -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,
})
}
+3 -2
View File
@@ -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)
+2 -2
View File
@@ -17,8 +17,8 @@
<div class="btn-group d-flex justify-content-end" role="group" aria-label="Operations">
<a href="/download/{{.Name}}/source" class="btn btn-primary btn-sm">Source</a>
<a href="/download/{{.Name}}/cartridge" class="btn btn-primary btn-sm">Cartridge</a>
<a href="/releases/{{.Name}}" class="btn btn-primary btn-sm">Releases</a>
<a href="/play/{{.Name}}" class="btn btn-primary btn-sm">Play</a>
<a href="/software/{{.Name}}" class="btn btn-primary btn-sm">Releases</a>
<a href="/play/{{.Name}}/{{.LatestRelease.Version}}" class="btn btn-primary btn-sm">Play</a>
</div>
</div>
</div>
@@ -7,11 +7,11 @@
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">Version: {{.Version}}</h5>
<h6 class="card-subtitle mb-2 text-muted">Released At: {{.CreatedAt.Format "2006-01-02 15:04:05"}}</h6>
<p class="card-subtitle mb-2 text-muted">Released At: {{.CreatedAt.Format "2006-01-02 15:04:05"}}</p>
</div>
<div class="card-footer">
<div class="btn-group d-flex justify-content-center" role="group" aria-label="Operations">
{{if .WebPlayable}}
{{if .HTMLFolderPath != ""}}
<a href="/play/{{$.Software.Name}}/{{.Version}}" class="btn btn-success btn-sm">Play</a>
{{end}}
<a href="/download/{{$.Software.Name}}/{{.Version}}/source" class="btn btn-primary btn-sm">Source</a>