Change to HTTP Model (#5)
This commit is contained in:
committed by
GitHub
parent
fe0d09d582
commit
63fc025070
41
cmd/star.go
41
cmd/star.go
@@ -1,17 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
func (c *Client) Star() error {
|
||||
project, _, err := c.git.Projects.StarProject(c.projectId)
|
||||
func (c *Client) Star() (*gitlab.Project, int, error) {
|
||||
project, res, err := c.git.Projects.StarProject(c.projectId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Starring project failed: %w", err)
|
||||
return nil, res.Response.StatusCode, fmt.Errorf("Starring project failed: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Success! Starred project: %s", project.Name)
|
||||
return project, http.StatusOK, nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func StarHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
client := r.Context().Value("client").(Client)
|
||||
project, status, err := client.Star()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err != nil {
|
||||
response := ErrorResponse{
|
||||
Message: err.Error(),
|
||||
Status: status,
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
|
||||
response := SuccessResponse{
|
||||
Message: fmt.Sprintf("Starred project %s successfully!", project.Name),
|
||||
Status: http.StatusOK,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user