diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 9e0d90e..157477b 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -4,9 +4,10 @@ diff --git a/controllers/clan_controller.go b/controllers/clan_controller.go index 23572de..f99bcae 100644 --- a/controllers/clan_controller.go +++ b/controllers/clan_controller.go @@ -31,8 +31,8 @@ func GetAllClans(c *gin.Context) { c.JSON(http.StatusOK, clans) } -// GetAllClanOptions GET /clan_options -func GetAllClanOptions(c *gin.Context) { +// GetAllClansHTML GET /clans_html +func GetAllClansHTML(c *gin.Context) { var clans []models.Clan models.DB.Find(&clans) diff --git a/controllers/player_controller.go b/controllers/player_controller.go index 6710f86..dd3e34e 100644 --- a/controllers/player_controller.go +++ b/controllers/player_controller.go @@ -2,10 +2,26 @@ package controllers import ( "InfrantrySkillCalculator/models" + "InfrantrySkillCalculator/utils" + "fmt" "github.com/gin-gonic/gin" + "gorm.io/gorm" + "log" "net/http" + "os" + "strconv" ) +type AddPlayerInput struct { + Name string `json:"name" binding:"required"` + ClanID uint `json:"clan_id" binding:"required"` +} + +type UpdatePlayerInput struct { + Name string `json:"name"` + ClanID uint `json:"clan_id"` +} + // GetAllPlayers GET /player func GetAllPlayers(c *gin.Context) { var players []models.Player @@ -13,3 +29,154 @@ func GetAllPlayers(c *gin.Context) { c.JSON(http.StatusOK, players) } + +// GetPlayersByClanHTML GET /players_html +func GetPlayersByClanHTML(c *gin.Context) { + var players []models.Player + clanId := c.Request.URL.Query().Get("id") + if err := models.DB.Where("clan_id = ?", utils.StringToUint(clanId)).Find(&players).Error; err != nil { + return + } + + file, err := os.ReadFile("./templates/player_list_item.html") + if err != nil { + return + } + playerItem := string(file) + //player_item = strings.Replace(player_item, "PLAYERNAME", player.Name, 1) + + var htmlOptions string + for _, player := range players { + htmlOptions += fmt.Sprintf(playerItem, player.Name) + } + + c.Header("Content-Type", "text/html") + c.String(http.StatusOK, htmlOptions) +} + +// AddPlayer POST /player +func AddPlayer(c *gin.Context) { + var input AddPlayerInput + if err := c.BindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + var player models.Player + if err := models.DB.Where("name = ?", input.Name).First(&player).Error; err == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Player with this name already exists!"}) + return + } + + player = models.Player{Name: input.Name, ClanID: input.ClanID} + models.DB.Create(&player) + + //UpdatePlayerTimestamp() + + c.JSON(http.StatusOK, player) + + _, err := fmt.Fprintf(utils.GinWriter, "Added player '"+player.Name+"'\n") + if err != nil { + log.Fatal(err) + } +} + +// GetPlayerByID GET /player/:id +func GetPlayerByID(c *gin.Context) { + player := FindPlayerByID(utils.StringToUint(c.Param("id"))) + if player == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + c.JSON(http.StatusOK, player) +} + +// GetPlayerIDByName GET /playerid/:name +func GetPlayerIDByName(c *gin.Context) { + var player models.Player + + if err := FindPlayerByName(&player, c).Error; err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + c.JSON(http.StatusOK, player.ID) +} + +// UpdatePlayerByID PATCH /player/:id +func UpdatePlayerByID(c *gin.Context) { + player := FindPlayerByID(utils.StringToUint(c.Param("id"))) + if player == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + var input UpdatePlayerInput + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + if player.Name != input.Name { + if err := FindPlayerByName(&player, c).Error; err == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Player with this name already exists!"}) + return + } + } + + msg := "Updating player '" + player.Name + "'#" + strconv.FormatUint(uint64(player.ID), 10) + if player.Name != input.Name { + msg += " (new: '" + input.Name + "')" + } + msg += " with clan #" + utils.UintToString(player.ClanID) + if player.ClanID != input.ClanID { + msg += " (new: ä" + utils.UintToString(input.ClanID) + ")" + } + _, err := fmt.Fprintf(utils.GinWriter, msg+"\n") + if err != nil { + log.Fatal(err) + } + + models.DB.Model(&player).Updates(map[string]interface{}{ + "Name": input.Name, + "ClanID": input.ClanID, + }) + + //UpdatePlayerTimestamp() + + c.JSON(http.StatusOK, player) +} + +// DeletePlayerByID DELETE /player/:id +func DeletePlayerByID(c *gin.Context) { + player := FindPlayerByID(utils.StringToUint(c.Param("id"))) + if player == nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"}) + return + } + + models.DB.Delete(&player) + + //UpdatePlayerTimestamp() + + c.JSON(http.StatusOK, true) + + _, err := fmt.Fprintf(utils.GinWriter, "Deleted player '"+player.Name+"'\n") + if err != nil { + log.Fatal(err) + } +} + +func FindPlayerByID(id uint) *models.Player { + var res models.Player + if err := models.DB.Where("id = ?", id).First(&res).Error; err != nil { + return nil + } else { + return &res + } +} + +func FindPlayerByName(out interface{}, c *gin.Context) *gorm.DB { + return models.DB.Where("name = ?", c.Param("name")).First(out) +} diff --git a/isc_data.db b/isc_data.db index 133885f..c2658a3 100644 Binary files a/isc_data.db and b/isc_data.db differ diff --git a/main.go b/main.go index 86bd75d..a6e8644 100644 --- a/main.go +++ b/main.go @@ -32,8 +32,6 @@ func main() { "./templates/index.html", "./templates/components/home_clan_bar.html", "./templates/components/opp_clan_bar.html", - "./templates/components/home_player_bar.html", - "./templates/components/opp_player_bar.html", "./templates/components/home_player_list.html", "./templates/components/opp_player_list.html", "./templates/components/bottom_controls.html", @@ -62,13 +60,19 @@ func main() { }) router.GET("/clans", controllers.GetAllClans) - router.GET("/clan_options", controllers.GetAllClanOptions) + router.GET("/clans_html", controllers.GetAllClansHTML) router.GET("/clan/:id", controllers.GetClanByID) router.POST("/clan", controllers.AddClan) router.PATCH("/clan/:id", controllers.UpdateClanByID) router.DELETE("/clan/:id", controllers.DeleteClanByID) router.GET("/players", controllers.GetAllPlayers) + router.GET("/players_html", controllers.GetPlayersByClanHTML) + router.GET("/player/:id", controllers.GetPlayerByID) + router.GET("/playerid/:name", controllers.GetPlayerIDByName) + router.POST("/player", controllers.AddPlayer) + router.PATCH("/player/:id", controllers.UpdatePlayerByID) + router.DELETE("/player/:id", controllers.DeletePlayerByID) log.Println("Running on 8000...") log.Fatal(router.Run(":8000")) diff --git a/static/index.js b/static/index.js index f268b54..4f5c90a 100644 --- a/static/index.js +++ b/static/index.js @@ -9,21 +9,19 @@ function setupClanButtons(dropdownId, delBtnId, editBtnId) { }); } -function setupPlayerButtons(dropdownId, listId, delBtnId, editBtnId, addBtnId) { +function getSelectedClanId(dropdownId) { + const dropdown = document.getElementById(dropdownId); + const selectedClanId = dropdown.options[dropdown.selectedIndex].value; + return parseInt(selectedClanId); +} + +function setupPlayerButtons(dropdownId, listId, addBtnId) { const dropdown = document.getElementById(dropdownId); - const deleteButton = document.getElementById(delBtnId); - const editButton = document.getElementById(editBtnId); const addButton = document.getElementById(addBtnId); dropdown.addEventListener('change', function () { - deleteButton.disabled = !this.value; - editButton.disabled = !this.value && (dropdown.selectedIndex !== -1); addButton.disabled = !this.value && (dropdown.selectedIndex !== -1); - }); - - dropdown.addEventListener('change', function () { - deleteButton.disabled = (this.selectedIndex !== -1); - editButton.disabled = (this.selectedIndex !== -1); + addButton.classList.toggle("bg-secondary-subtle"); }); } diff --git a/templates/components/home_clan_bar.html b/templates/components/home_clan_bar.html index c217087..2719ac6 100644 --- a/templates/components/home_clan_bar.html +++ b/templates/components/home_clan_bar.html @@ -6,7 +6,7 @@
- @@ -26,7 +26,7 @@ diff --git a/templates/components/home_player_bar.html b/templates/components/home_player_bar.html deleted file mode 100644 index 2e2080d..0000000 --- a/templates/components/home_player_bar.html +++ /dev/null @@ -1,34 +0,0 @@ -{{ define "home_player_bar" }} - -
-
-
- - -
-
-
- 0 von 0 ausgewählt -
-
-
- - - -
-
-
- - - -{{ end }} \ No newline at end of file diff --git a/templates/components/home_player_list.html b/templates/components/home_player_list.html index 306ae03..cc35f48 100644 --- a/templates/components/home_player_list.html +++ b/templates/components/home_player_list.html @@ -1,8 +1,32 @@ {{ define "home_player_list" }} - - +
+
+
+ +
+
+
+
+ + +
+
+ +
+
+ + 0 +
+
+
+ + {{ end }} \ No newline at end of file diff --git a/templates/components/opp_clan_bar.html b/templates/components/opp_clan_bar.html index 2f44920..b1fcf62 100644 --- a/templates/components/opp_clan_bar.html +++ b/templates/components/opp_clan_bar.html @@ -6,7 +6,7 @@
- - -
-
-
- 0 von 0 ausgewählt -
-
-
- - - -
-
-
- - - -{{ end }} \ No newline at end of file diff --git a/templates/components/opp_player_list.html b/templates/components/opp_player_list.html index 2368bab..e634c28 100644 --- a/templates/components/opp_player_list.html +++ b/templates/components/opp_player_list.html @@ -1,8 +1,32 @@ {{ define "opp_player_list" }} - - +
+
+
+ +
+
+
+
+ + +
+
+ +
+
+ + 0 +
+
+
+ + {{ end }} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index f68b600..5e7d80a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,7 +3,7 @@ {{ template "header" . }} - +
@@ -14,9 +14,6 @@ {{ template "home_player_list" . }} - - - {{ template "home_player_bar" . }}
@@ -27,9 +24,6 @@ {{ template "opp_player_list" . }} - - - {{ template "opp_player_bar" . }}
diff --git a/templates/modals/add_player.html b/templates/modals/add_player.html index 19052ff..f7ce82b 100644 --- a/templates/modals/add_player.html +++ b/templates/modals/add_player.html @@ -9,8 +9,8 @@