| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
|
| 6 |
"github.com/gin-gonic/gin" |
| 7 |
"github.com/henrybear327/go-proton-api" |
| 8 |
"golang.org/x/exp/slices" |
| 9 |
) |
| 10 |
|
| 11 |
func (s *Server) handleGetAddresses() gin.HandlerFunc { |
| 12 |
return func(c *gin.Context) { |
| 13 |
addresses, err := s.b.GetAddresses(c.GetString("UserID")) |
| 14 |
if err != nil { |
| 15 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 16 |
return |
| 17 |
} |
| 18 |
|
| 19 |
c.JSON(http.StatusOK, gin.H{ |
| 20 |
"Addresses": addresses, |
| 21 |
}) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func (s *Server) handleGetAddress() gin.HandlerFunc { |
| 26 |
return func(c *gin.Context) { |
| 27 |
address, err := s.b.GetAddress(c.GetString("UserID"), c.Param("addressID")) |
| 28 |
if err != nil { |
| 29 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
c.JSON(http.StatusOK, gin.H{ |
| 34 |
"Address": address, |
| 35 |
}) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func (s *Server) handlePutAddressEnable() gin.HandlerFunc { |
| 40 |
return func(c *gin.Context) { |
| 41 |
if err := s.b.EnableAddress(c.GetString("UserID"), c.Param("addressID")); err != nil { |
| 42 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 43 |
return |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func (s *Server) handlePutAddressDisable() gin.HandlerFunc { |
| 49 |
return func(c *gin.Context) { |
| 50 |
if err := s.b.DisableAddress(c.GetString("UserID"), c.Param("addressID")); err != nil { |
| 51 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 52 |
return |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func (s *Server) handleDeleteAddress() gin.HandlerFunc { |
| 58 |
return func(c *gin.Context) { |
| 59 |
if err := s.b.DeleteAddress(c.GetString("UserID"), c.Param("addressID")); err != nil { |
| 60 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 61 |
return |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func (s *Server) handlePutAddressesOrder() gin.HandlerFunc { |
| 67 |
return func(c *gin.Context) { |
| 68 |
var req proton.OrderAddressesReq |
| 69 |
|
| 70 |
if err := c.BindJSON(&req); err != nil { |
| 71 |
c.AbortWithStatus(http.StatusBadRequest) |
| 72 |
return |
| 73 |
} |
| 74 |
|
| 75 |
addresses, err := s.b.GetAddresses(c.GetString("UserID")) |
| 76 |
if err != nil { |
| 77 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
if len(req.AddressIDs) != len(addresses) { |
| 82 |
c.AbortWithStatus(http.StatusBadRequest) |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
for _, address := range addresses { |
| 87 |
if !slices.Contains(req.AddressIDs, address.ID) { |
| 88 |
c.AbortWithStatus(http.StatusBadRequest) |
| 89 |
return |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if err := s.b.SetAddressOrder(c.GetString("UserID"), req.AddressIDs); err != nil { |
| 94 |
c.AbortWithStatus(http.StatusInternalServerError) |
| 95 |
return |
| 96 |
} |
| 97 |
} |
| 98 |
} |