diff --git a/astro/services/Competition.go b/astro/services/Competition.go index c3ad4a60495c59df3667e236c20d5c24393b8e40..2782611b7a29be4d802668b490075bf612a0382e 100644 --- a/astro/services/Competition.go +++ b/astro/services/Competition.go @@ -1,5 +1,7 @@ package services +import "strconv" + // Competition : Competition details type Competition struct { CompetitionID uint8 // 255 competitions max @@ -14,10 +16,10 @@ type Competition struct { } func (c *Competition) String() string { - return "Competition : " + c.CompetitionName + " - " + c.CompetitionCategory.String() + " - " + c.CompetitionWeapon.String() + " - " + c.CompetitionState.String() + return "Competition : id:" + strconv.Itoa(int(c.CompetitionID)) + " - " + c.CompetitionName + " - " + c.CompetitionCategory.String() + " - " + c.CompetitionWeapon.String() + " - " + c.CompetitionState.String() } -func CreateCompetition(competitionID uint8, competitionName string, competitionCategory Category, competitionWeapon Weapon, competitionMaxStageNumber uint8) Competition { +func CreateCompetition(competitionID uint8, competitionName string, competitionCategory Category, competitionWeapon Weapon, competitionMaxStageNumber uint8) *Competition { var c Competition c.CompetitionID = competitionID @@ -31,7 +33,7 @@ func CreateCompetition(competitionID uint8, competitionName string, competitionC c.InitCompetition() - return c + return &c } func (c *Competition) InitCompetition() bool { diff --git a/astro/services/Session.go b/astro/services/Session.go index 8a19405a0ac4dfcd3243069a1dea58c27462de78..115ab511831b21c28ebaa909ae009cf5b97f387b 100644 --- a/astro/services/Session.go +++ b/astro/services/Session.go @@ -1,36 +1,45 @@ package services -import "fmt" - const MinStageSize = 3 // Session : Session details type Session struct { - Competitions [](*Competition) + Competitions map[uint8]*Competition CompetitionNumber uint8 } -func (s *Session) AddCompetition(name string, category string, weapon string) bool { - fmt.Println("Adding competition") +func (s *Session) AddCompetition(name string, category string, weapon string) *Competition { + + id := uint8(0) + + for { + // Check if the competition already exists + if _, ok := s.Competitions[id]; !ok { + break + } + id++ + } + competition := CreateCompetition( - s.CompetitionNumber, + id, name, CreateCategory(category), CreateWeapon(weapon), MinStageSize) - s.Competitions = append(s.Competitions, &competition) + s.Competitions[competition.CompetitionID] = competition s.CompetitionNumber++ - return true + return competition } func (s *Session) RemoveCompetition(competitionID uint8) bool { - for i, competition := range s.Competitions { - if competition.CompetitionID == competitionID { - s.Competitions = append(s.Competitions[:i], s.Competitions[i+1:]...) - } + if _, ok := s.Competitions[competitionID]; !ok { + return false } + + delete(s.Competitions, competitionID) + s.CompetitionNumber-- return true @@ -52,10 +61,3 @@ func (s *Session) GetCompetition(competitionID uint8) *Competition { } return nil } - -func (s *Session) GetLastCompetition() *Competition { - if len(s.Competitions) > 0 { - return s.Competitions[len(s.Competitions)-1] - } - return nil -} diff --git a/components/Nav.templ b/components/Nav.templ index 01f0ea3192e45d31dabba339649c0f1005a2eb64..a3feb779fb7fda84ae612a7fdea56e2baff2e242 100644 --- a/components/Nav.templ +++ b/components/Nav.templ @@ -1,12 +1,21 @@ package components -import "astroproject/astro/services" +import ( + "astroproject/astro/services" + "fmt" +) templ CompetitionElement(competition *services.Competition) { // Onclick, redirect to the competition page - <div class="competition-el" hx-on:click={ Redirect(competition.CompetitionID) }> - <span>{ competition.CompetitionName }</span> - <button>X</button> + <div class="competition-el"> + <span hx-on:click={ Redirect(competition.CompetitionID) }>{ competition.CompetitionName }</span> + <button + hx-post={ fmt.Sprintf("/delete-competition/%d", competition.CompetitionID) } + hx-target="closest .competition-el" + hx-swap="delete" + class="rounded-full border border-indigo-600 bg-indigo-600 p-3 text-white hover:bg-transparent hover:text-indigo-600 focus:outline-none focus:ring active:text-indigo-500 aspect-square" + hx-on:click={ RedirectHome() } + >X</button> </div> } @@ -14,6 +23,10 @@ script Redirect(competitionID uint8) { window.location.href = "/competition/" + competitionID } +script RedirectHome() { + window.location.href = "/" +} + templ Nav(session *services.Session) { <div class="nav-bar" @@ -29,6 +42,7 @@ templ Nav(session *services.Session) { hx-post="/add-competition" hx-target=".competition-container" hx-swap="beforeend" + class="rounded border border-indigo-600 bg-indigo-600 px-12 py-3 text-sm font-medium text-white hover:bg-transparent hover:text-indigo-600 focus:outline-none focus:ring active:text-indigo-500" >Add competition</button> </div> </div> diff --git a/main.go b/main.go index 7631eff8bfd67a26076343ccebc3093fb9b24659..670bdfd4b273594be7363600e8868ab94ce1b71e 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,10 @@ var assets embed.FS func main() { - session := services.Session{} - session.AddCompetition("Competition 1", "U7", "Foil") + session := services.Session{ + CompetitionNumber: 0, + Competitions: map[uint8]*services.Competition{}, + } r := routes.NewChiRouter(&session) app := application.New(application.Options{ diff --git a/pages/Competition.templ b/pages/Competition.templ index 1989fbf7556245379bb46f6d3be044f576a09261..635a3dbda00385b617a22f2c9bee6d9ddae4d5d9 100644 --- a/pages/Competition.templ +++ b/pages/Competition.templ @@ -1,9 +1,13 @@ package pages -import "astroproject/astro/services" +import ( + "astroproject/astro/services" + "strconv" +) templ CompetitionPage(competition *services.Competition) { <h1>Competition Page</h1> + <h1>->{ strconv.Itoa(int(competition.CompetitionID)) }</h1> <h1>{ competition.CompetitionName }</h1> <h2>{ competition.CompetitionCategory.String() }</h2> } diff --git a/routes/app.go b/routes/app.go index b350158af67071fb004b8923ac37b2715b773f70..59e387215d323f04b702702dbaca44791941860c 100644 --- a/routes/app.go +++ b/routes/app.go @@ -7,6 +7,7 @@ import ( "github.com/a-h/templ" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" + "github.com/go-faker/faker/v4" "astroproject/astro/services" "astroproject/components" @@ -28,11 +29,27 @@ func NewChiRouter(session *services.Session) *chi.Mux { }) r.Post("/add-competition", func(w http.ResponseWriter, r *http.Request) { - if session.AddCompetition("Competition", "U20", "Foil") { - templ.Handler(components.CompetitionElement(session.GetLastCompetition())).ServeHTTP(w, r) + competition := session.AddCompetition(faker.FirstName(), "U20", "Foil") + + if competition != nil { + templ.Handler(components.CompetitionElement(competition)).ServeHTTP(w, r) } }) + r.Post("/delete-competition/{competitionID}", func(w http.ResponseWriter, r *http.Request) { + competitionIDStr := chi.URLParam(r, "competitionID") + competitionID, err := strconv.ParseUint(competitionIDStr, 10, 8) + if err != nil { + // Return a 404. + http.NotFound(w, r) + } + + session.RemoveCompetition(uint8(competitionID)) + + // Return a 200. + w.WriteHeader(http.StatusOK) + }) + r.Get("/competition/{competitionID}", func(w http.ResponseWriter, r *http.Request) { competitionIDStr := chi.URLParam(r, "competitionID") competitionID, err := strconv.ParseUint(competitionIDStr, 10, 8)