update.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package database
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. type DatabaseUpdateStatusHandler struct {
  12. handlers.PorterHandlerReader
  13. }
  14. func NewDatabaseUpdateStatusHandler(
  15. config *config.Config,
  16. decoderValidator shared.RequestDecoderValidator,
  17. ) *DatabaseUpdateStatusHandler {
  18. return &DatabaseUpdateStatusHandler{
  19. PorterHandlerReader: handlers.NewDefaultPorterHandler(config, decoderValidator, nil),
  20. }
  21. }
  22. func (p *DatabaseUpdateStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  23. // read the project from context
  24. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  25. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  26. req := &types.UpdateDatabaseStatusRequest{}
  27. if ok := p.DecodeAndValidate(w, r, req); !ok {
  28. return
  29. }
  30. // read all clusters for this project
  31. db, err := p.Repo().Database().ReadDatabaseByInfraID(proj.ID, infra.ID)
  32. if err != nil {
  33. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  34. return
  35. }
  36. db.Status = req.Status
  37. db, err = p.Repo().Database().UpdateDatabase(db)
  38. if err != nil {
  39. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  40. return
  41. }
  42. }