Skip to content

Commit

Permalink
Merge pull request #1 from thanthtooaung-coding/release/ver-1.1
Browse files Browse the repository at this point in the history
Add Category and fix some S3 PutObject configuration
  • Loading branch information
thanthtooaung-coding authored Nov 16, 2024
2 parents 680cf87 + f2ac08a commit 47d4a9c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
49 changes: 49 additions & 0 deletions handlers/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,52 @@ func DeleteProject(c *gin.Context) {

c.JSON(http.StatusOK, gin.H{"message": "Project deleted"})
}

func UpdateProject(c *gin.Context) {
title := c.Param("title")

var updatedProject models.Project
if err := c.ShouldBindJSON(&updatedProject); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}

data, err := s3Client.GetObject(s3Key)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch existing projects", "details": err.Error()})
return
}

var projects []models.Project
if err := json.Unmarshal(data, &projects); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid data format"})
return
}

projectUpdated := false
for i, project := range projects {
if project.Title == title {
projects[i] = updatedProject
projectUpdated = true
break
}
}

if !projectUpdated {
c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"})
return
}

updatedData, err := json.Marshal(projects)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to process updated projects", "details": err.Error()})
return
}

if err := s3Client.PutObject(s3Key, updatedData); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save updated projects"})
return
}

c.JSON(http.StatusOK, gin.H{"message": "Project updated successfully", "project": updatedProject})
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {
r.GET("/projects", handlers.GetProjects)
r.POST("/projects", handlers.AddProject)
r.DELETE("/projects/:title", handlers.DeleteProject)
r.PUT("/projects/:title", handlers.UpdateProject)

r.Run(":8080")
}
1 change: 1 addition & 0 deletions models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Project struct {
Title string `json:"title"`
GithubLink string `json:"githubLink"`
ImgUrl string `json:"imgUrl"`
Category string `json:"category"`
}
15 changes: 1 addition & 14 deletions utils/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"

Expand Down Expand Up @@ -55,19 +54,7 @@ func (s *S3Client) PutObject(key string, content []byte) error {
_, err := s.Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(key),
Body: ioutil.NopCloser(bytes.NewReader(content)),
Body: bytes.NewReader(content),
})
return err
}

func (s *S3Client) UploadFile(key string, body io.Reader) (string, error) {
_, err := s.Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(key),
Body: ioutil.NopCloser(body),
})
if err != nil {
return "", fmt.Errorf("failed to upload file: %v", err)
}
return fmt.Sprintf("https://%s.s3.amazonaws.com/%s", s.Bucket, key), nil
}

0 comments on commit 47d4a9c

Please sign in to comment.