account.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package apiController
  2. import (
  3. internalDbInfra "github.com/goinfinite/os/src/infra/internalDatabase"
  4. apiHelper "github.com/goinfinite/os/src/presentation/api/helper"
  5. "github.com/goinfinite/os/src/presentation/liaison"
  6. "github.com/labstack/echo/v4"
  7. )
  8. type AccountController struct {
  9. accountLiaison *liaison.AccountLiaison
  10. }
  11. func NewAccountController(
  12. persistentDbSvc *internalDbInfra.PersistentDatabaseService,
  13. trailDbSvc *internalDbInfra.TrailDatabaseService,
  14. ) *AccountController {
  15. return &AccountController{
  16. accountLiaison: liaison.NewAccountLiaison(persistentDbSvc, trailDbSvc),
  17. }
  18. }
  19. // ReadAccounts godoc
  20. // @Summary ReadAccounts
  21. // @Description List accounts.
  22. // @Tags account
  23. // @Accept json
  24. // @Produce json
  25. // @Security Bearer
  26. // @Param id query string false "Id"
  27. // @Param username query string false "Username"
  28. // @Param shouldIncludeSecureAccessPublicKeys query bool false "ShouldIncludeSecureAccessPublicKeys (only works if OpenSSH service is installed)"
  29. // @Param pageNumber query uint false "PageNumber (Pagination)"
  30. // @Param itemsPerPage query uint false "ItemsPerPage (Pagination)"
  31. // @Param sortBy query string false "SortBy (Pagination)"
  32. // @Param sortDirection query string false "SortDirection (Pagination)"
  33. // @Param lastSeenId query string false "LastSeenId (Pagination)"
  34. // @Success 200 {object} dto.ReadAccountsResponse
  35. // @Router /v1/account/ [get]
  36. func (controller *AccountController) Read(c echo.Context) error {
  37. requestInputData, err := apiHelper.ReadRequestInputData(c)
  38. if err != nil {
  39. return err
  40. }
  41. return apiHelper.LiaisonResponseWrapper(
  42. c, controller.accountLiaison.Read(requestInputData),
  43. )
  44. }
  45. // CreateAccount godoc
  46. // @Summary CreateAccount
  47. // @Description Create a new account.
  48. // @Tags account
  49. // @Accept json
  50. // @Produce json
  51. // @Security Bearer
  52. // @Param createAccountDto body dto.CreateAccount true "All props are required."
  53. // @Success 201 {object} object{} "AccountCreated"
  54. // @Router /v1/account/ [post]
  55. func (controller *AccountController) Create(c echo.Context) error {
  56. requestInputData, err := apiHelper.ReadRequestInputData(c)
  57. if err != nil {
  58. return err
  59. }
  60. return apiHelper.LiaisonResponseWrapper(
  61. c, controller.accountLiaison.Create(requestInputData),
  62. )
  63. }
  64. // UpdateAccount godoc
  65. // @Summary UpdateAccount
  66. // @Description Update an account.
  67. // @Tags account
  68. // @Accept json
  69. // @Produce json
  70. // @Security Bearer
  71. // @Param updateDto body dto.UpdateAccount true "Only id or username is required."
  72. // @Success 200 {object} object{} "'AccountUpdated' message or new API key in string format"
  73. // @Router /v1/account/ [put]
  74. func (controller *AccountController) Update(c echo.Context) error {
  75. requestInputData, err := apiHelper.ReadRequestInputData(c)
  76. if err != nil {
  77. return err
  78. }
  79. return apiHelper.LiaisonResponseWrapper(
  80. c, controller.accountLiaison.Update(requestInputData),
  81. )
  82. }
  83. // DeleteAccount godoc
  84. // @Summary DeleteAccount
  85. // @Description Delete an account.
  86. // @Tags account
  87. // @Accept json
  88. // @Produce json
  89. // @Security Bearer
  90. // @Param accountId path string true "AccountId to delete."
  91. // @Success 200 {object} object{} "AccountDeleted"
  92. // @Router /v1/account/{accountId}/ [delete]
  93. func (controller *AccountController) Delete(c echo.Context) error {
  94. requestInputData, err := apiHelper.ReadRequestInputData(c)
  95. if err != nil {
  96. return err
  97. }
  98. return apiHelper.LiaisonResponseWrapper(
  99. c, controller.accountLiaison.Delete(requestInputData),
  100. )
  101. }
  102. // CreateSecureAccessPublicKey godoc
  103. // @Summary CreateSecureAccessPublicKey
  104. // @Description Create a new secure access public key.
  105. // @Tags account
  106. // @Accept json
  107. // @Produce json
  108. // @Security Bearer
  109. // @Param createSecureAccessPublicKey body dto.CreateSecureAccessPublicKey true "'name' is optional. Will only become required if there is no name in 'content'. If the 'name' is provided, it will overwrite the name in the 'content'."
  110. // @Success 201 {object} object{} "SecureAccessPublicKeyCreated"
  111. // @Router /v1/account/secure-access-public-key/ [post]
  112. func (controller *AccountController) CreateSecureAccessPublicKey(c echo.Context) error {
  113. requestInputData, err := apiHelper.ReadRequestInputData(c)
  114. if err != nil {
  115. return err
  116. }
  117. return apiHelper.LiaisonResponseWrapper(
  118. c, controller.accountLiaison.CreateSecureAccessPublicKey(requestInputData),
  119. )
  120. }
  121. // DeleteSecureAccessPublicKey godoc
  122. // @Summary DeleteSecureAccessPublicKey
  123. // @Description Delete a secure access public key.
  124. // @Tags account
  125. // @Accept json
  126. // @Produce json
  127. // @Security Bearer
  128. // @Param secureAccessPublicKeyId path string true "SecureAccessPublicKeyId to delete."
  129. // @Success 200 {object} object{} "SecureAccessPublicKeyDeleted"
  130. // @Router /v1/account/secure-access-public-key/{secureAccessPublicKeyId}/ [delete]
  131. func (controller *AccountController) DeleteSecureAccessPublicKey(c echo.Context) error {
  132. requestInputData, err := apiHelper.ReadRequestInputData(c)
  133. if err != nil {
  134. return err
  135. }
  136. return apiHelper.LiaisonResponseWrapper(
  137. c, controller.accountLiaison.DeleteSecureAccessPublicKey(requestInputData),
  138. )
  139. }