Skip to content
Snippets Groups Projects
Commit 30df59a3 authored by ForkBench's avatar ForkBench
Browse files

Adding / Removing competition working.

parent 88175936
No related branches found
No related tags found
No related merge requests found
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 {
......
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
}
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>
......
......@@ -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{
......
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>
}
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment