Skip to content

Instantly share code, notes, and snippets.

@krittawatcode
Last active November 20, 2020 18:16
Show Gist options
  • Save krittawatcode/b94ecc07a393e2bf5e747141db637dff to your computer and use it in GitHub Desktop.
Save krittawatcode/b94ecc07a393e2bf5e747141db637dff to your computer and use it in GitHub Desktop.
package service
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/krittawatcode/go-soldier-mvc/cores"
)
// DutyService for define all service
type DutyService interface {
EatTax(c *gin.Context, commission int)
}
type soldierInfo struct {
Rank string `json:"rank"`
Wife int `json:"wife"`
Salary int `json:"salary"`
Home bool `json:"home"`
Car bool `json:"car"`
Corruption bool `json:"corruption"`
}
// SoldierDutyService work like a contructer in java
func SoldierDutyService(c *gin.Context) DutyService {
var soldier cores.Solider
if err := c.ShouldBind(&soldier); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
}
return &soldierInfo{
Rank: soldier.Rank,
Wife: soldier.Wife,
Salary: soldier.Salary,
Home: soldier.Home,
Car: soldier.Car,
Corruption: soldier.Corruption,
}
}
func (s *soldierInfo) EatTax(c *gin.Context, commission int) {
if s.Rank != "General" { // Field Marshall ??
s.Salary -= commission
}
if s.Corruption == true {
s.getPromote("Elite")
}
c.JSON(http.StatusOK, gin.H{"resp": s})
}
func (s *soldierInfo) getPromote(newrank string) {
s.Rank = newrank
s.Car = true
s.Home = true
s.Wife += 10
s.Salary += s.Salary * 100
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment