40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package update
|
|
|
|
import (
|
|
domain "RenewCMS/internal/domain/update"
|
|
"RenewCMS/internal/infrastructure/useCases"
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type Handler struct {
|
|
GetUpdateStatusUseCase *useCases.GetUpdateStatusUseCase
|
|
CheckForUpdateUseCase *useCases.CheckForUpdateUseCase
|
|
}
|
|
|
|
func NewHandler(getStatus *useCases.GetUpdateStatusUseCase, checkForUpdate *useCases.CheckForUpdateUseCase) *Handler {
|
|
return &Handler{GetUpdateStatusUseCase: getStatus, CheckForUpdateUseCase: checkForUpdate}
|
|
}
|
|
|
|
func (h *Handler) GetStatus(w http.ResponseWriter, _ *http.Request) {
|
|
writeStatus(w, h.GetUpdateStatusUseCase.GetStatus())
|
|
}
|
|
|
|
func (h *Handler) Check(w http.ResponseWriter, _ *http.Request) {
|
|
status, err := h.CheckForUpdateUseCase.Check()
|
|
if err != nil {
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeStatus(w, status)
|
|
}
|
|
|
|
func writeStatus(w http.ResponseWriter, status domain.UpdateStatus) {
|
|
body, err := json.Marshal(status)
|
|
if err != nil {
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
_, _ = w.Write(body)
|
|
}
|