2025-02-19 12:28:48 +01:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2025-02-20 12:46:24 +01:00
|
|
|
"oc-datacenter/infrastructure"
|
|
|
|
"slices"
|
|
|
|
|
2025-02-19 12:28:48 +01:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Operations about the admiralty objects of the datacenter
|
|
|
|
type AdmiraltyController struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Title GetAllTargets
|
|
|
|
// @Description find all Admiralty Target
|
2025-02-20 12:46:24 +01:00
|
|
|
// @Success 200
|
|
|
|
// @router /targets [get]
|
2025-02-19 12:28:48 +01:00
|
|
|
func (c *AdmiraltyController) GetAllTargets() {
|
2025-02-20 12:46:24 +01:00
|
|
|
serv, err := infrastructure.NewService()
|
|
|
|
if err != nil {
|
|
|
|
// change code to 500
|
|
|
|
c.Ctx.Output.SetStatus(500)
|
|
|
|
c.ServeJSON()
|
|
|
|
c.Data["json"] = map[string]string{"error": err.Error()}
|
|
|
|
return
|
|
|
|
}
|
2025-02-19 12:28:48 +01:00
|
|
|
|
2025-02-20 12:46:24 +01:00
|
|
|
res, err := serv.GetTargets(c.Ctx.Request.Context())
|
|
|
|
c.Data["json"] = res
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Title GetOneTarget
|
|
|
|
// @Description find one Admiralty Target
|
|
|
|
// @Param id path string true "the name of the target to get"
|
|
|
|
// @Success 200
|
|
|
|
// @router /targets/:id [get]
|
|
|
|
func (c *AdmiraltyController) GetOneTarget() {
|
|
|
|
id := c.Ctx.Input.Param(":id")
|
|
|
|
serv, err := infrastructure.NewService()
|
|
|
|
if err != nil {
|
|
|
|
// change code to 500
|
|
|
|
c.Ctx.Output.SetStatus(500)
|
|
|
|
c.ServeJSON()
|
|
|
|
c.Data["json"] = map[string]string{"error": err.Error()}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := serv.GetTargets(c.Ctx.Request.Context())
|
|
|
|
id = "target-"+id
|
|
|
|
found := slices.Contains(res,id)
|
|
|
|
if !found {
|
|
|
|
c.Ctx.Output.SetStatus(404)
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = id
|
|
|
|
c.ServeJSON()
|
2025-02-19 12:28:48 +01:00
|
|
|
}
|