Skip to content

Commit

Permalink
refactor | Implemented repository pattern to the APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
anveshthakur committed Aug 28, 2024
1 parent affbe23 commit f1e2e42
Show file tree
Hide file tree
Showing 18 changed files with 2,087 additions and 1,607 deletions.
35 changes: 26 additions & 9 deletions src/database/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,56 @@ package database

import (
"github.com/kevinanielsen/go-fast-cdn/src/models"
"gorm.io/gorm"
)

func GetDocByCheckSum(checksum []byte) models.Doc {
type DocRepo struct {
DB *gorm.DB
}

func NewDocRepo(db *gorm.DB) models.DocRepository {
return &DocRepo{DB: db}
}

func (repo *DocRepo) GetAllDocs() []models.Doc {
var entries []models.Doc

repo.DB.Find(&entries, &models.Doc{})

return entries
}

func (repo *DocRepo) GetDocByCheckSum(checksum []byte) models.Doc {
var entries models.Doc

DB.Where("checksum = ?", checksum).First(&entries)
repo.DB.Where("checksum = ?", checksum).First(&entries)

return entries
}

func AddDoc(doc models.Doc) (string, error) {
result := DB.Create(&doc)
func (repo *DocRepo) AddDoc(doc models.Doc) (string, error) {
result := repo.DB.Create(&doc)
if result.Error != nil {
return "", result.Error
}

return doc.FileName, result.Error
}

func DeleteDoc(fileName string) (string, bool) {
func (repo *DocRepo) DeleteDoc(fileName string) (string, bool) {
var doc models.Doc

result := DB.Where("file_name = ?", fileName).First(&doc)
result := repo.DB.Where("file_name = ?", fileName).First(&doc)

if result.Error == nil {
DB.Delete(&doc)
repo.DB.Delete(&doc)
return fileName, true
} else {
return "", false
}
}

func RenameDoc(oldFileName, newFileName string) error {
func (repo *DocRepo) RenameDoc(oldFileName, newFileName string) error {
doc := models.Doc{}
return DB.Model(&doc).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error
return repo.DB.Model(&doc).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error
}
35 changes: 26 additions & 9 deletions src/database/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,56 @@ package database

import (
"github.com/kevinanielsen/go-fast-cdn/src/models"
"gorm.io/gorm"
)

func GetImageByCheckSum(checksum []byte) models.Image {
type imageRepo struct {
DB *gorm.DB
}

func NewImageRepo(db *gorm.DB) models.ImageRepository {
return &imageRepo{DB: db}
}

func (repo *imageRepo) GetAllImages() []models.Image {
var entries []models.Image

repo.DB.Find(&entries, &models.Image{})

return entries
}

func (repo *imageRepo) GetImageByCheckSum(checksum []byte) models.Image {
var entries models.Image

DB.Where("checksum = ?", checksum).First(&entries)
repo.DB.Where("checksum = ?", checksum).First(&entries)

return entries
}

func AddImage(image models.Image) (string, error) {
result := DB.Create(&image)
func (repo *imageRepo) AddImage(image models.Image) (string, error) {
result := repo.DB.Create(&image)
if result.Error != nil {
return "", result.Error
}

return image.FileName, nil
}

func DeleteImage(fileName string) (string, bool) {
func (repo *imageRepo) DeleteImage(fileName string) (string, bool) {
var image models.Image

result := DB.Where("file_name = ?", fileName).First(&image)
result := repo.DB.Where("file_name = ?", fileName).First(&image)

if result.Error == nil {
DB.Delete(&image)
repo.DB.Delete(&image)
return fileName, true
} else {
return "", false
}
}

func RenameImage(oldFileName, newFileName string) error {
func (repo *imageRepo) RenameImage(oldFileName, newFileName string) error {
image := models.Image{}
return DB.Model(&image).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error
return repo.DB.Model(&image).Where("file_name = ?", oldFileName).Update("file_name", newFileName).Error
}
13 changes: 13 additions & 0 deletions src/handlers/docs/DocHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handlers

import (
"github.com/kevinanielsen/go-fast-cdn/src/models"
)

type DocHandler struct {
repo models.DocRepository
}

func NewDocHandler(repo models.DocRepository) *DocHandler {
return &DocHandler{repo}
}
8 changes: 2 additions & 6 deletions src/handlers/docs/handleAllDocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/models"
)

func HandleAllDocs(c *gin.Context) {
var entries []models.Doc

database.DB.Find(&entries, &models.Doc{})
func (h *DocHandler) HandleAllDocs(c *gin.Context) {
entries := h.repo.GetAllDocs()

c.JSON(http.StatusOK, entries)
}
5 changes: 2 additions & 3 deletions src/handlers/docs/handleDocDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/util"
)

func HandleDocDelete(c *gin.Context) {
func (h *DocHandler) HandleDocDelete(c *gin.Context) {
fileName := c.Param("filename")
if fileName == "" {
c.JSON(http.StatusBadRequest, gin.H{
Expand All @@ -17,7 +16,7 @@ func HandleDocDelete(c *gin.Context) {
return
}

deletedFileName, success := database.DeleteDoc(fileName)
deletedFileName, success := h.repo.DeleteDoc(fileName)
if !success {
c.JSON(http.StatusNotFound, gin.H{
"error": "Document not found",
Expand Down
7 changes: 3 additions & 4 deletions src/handlers/docs/handleDocUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"path/filepath"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/models"
"github.com/kevinanielsen/go-fast-cdn/src/util"
)

func HandleDocUpload(c *gin.Context) {
func (h *DocHandler) HandleDocUpload(c *gin.Context) {
fileHeader, err := c.FormFile("doc")
newName := c.PostForm("filename")

Expand Down Expand Up @@ -71,13 +70,13 @@ func HandleDocUpload(c *gin.Context) {
Checksum: fileHashBuffer[:],
}

docInDatabase := database.GetDocByCheckSum(fileHashBuffer[:])
docInDatabase := h.repo.GetDocByCheckSum(fileHashBuffer[:])
if len(docInDatabase.Checksum) > 0 {
c.JSON(http.StatusConflict, "File already exists")
return
}

savedFileName, err := database.AddDoc(doc)
savedFileName, err := h.repo.AddDoc(doc)
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
return
Expand Down
20 changes: 13 additions & 7 deletions src/handlers/docs/handleDocUpload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestHandleDocUpload_NoError(t *testing.T) {
}()

// handling
HandleDocUpload(c)
docHandler := NewDocHandler(database.NewDocRepo(database.DB))
docHandler.HandleDocUpload(c)

// assert
require.Equal(t, http.StatusOK, w.Result().StatusCode)
Expand Down Expand Up @@ -81,7 +82,8 @@ func TestHandleDocUpload_ReadFailed_NoFile(t *testing.T) {
}()

// handling
HandleDocUpload(c)
docHandler := NewDocHandler(database.NewDocRepo(database.DB))
docHandler.HandleDocUpload(c)

// assert
require.Equal(t, http.StatusBadRequest, w.Result().StatusCode)
Expand Down Expand Up @@ -117,7 +119,8 @@ func TestHandleDocUpload_ReadFailed_EOF(t *testing.T) {
}()

// handling
HandleDocUpload(c)
docHandler := NewDocHandler(database.NewDocRepo(database.DB))
docHandler.HandleDocUpload(c)

// assert
require.Equal(t, http.StatusInternalServerError, w.Result().StatusCode)
Expand Down Expand Up @@ -153,7 +156,8 @@ func TestHandleDocUpload_InvalidType(t *testing.T) {
}()

// handling
HandleDocUpload(c)
docHandler := NewDocHandler(database.NewDocRepo(database.DB))
docHandler.HandleDocUpload(c)

// assert
require.Equal(t, http.StatusBadRequest, w.Result().StatusCode)
Expand Down Expand Up @@ -189,7 +193,8 @@ func TestHandleDocUpload_InvalidFilename(t *testing.T) {
}()

// handling
HandleDocUpload(c)
docHandler := NewDocHandler(database.NewDocRepo(database.DB))
docHandler.HandleDocUpload(c)

// assert
require.Equal(t, http.StatusBadRequest, w.Result().StatusCode)
Expand Down Expand Up @@ -225,7 +230,8 @@ func TestHandleDocUpload_FileExist(t *testing.T) {
c.Request.Header.Add("Content-Type", writer.FormDataContentType())

// first handling
HandleDocUpload(c)
docHandler := NewDocHandler(database.NewDocRepo(database.DB))
docHandler.HandleDocUpload(c)

// first statement
require.Equal(t, http.StatusOK, w.Result().StatusCode)
Expand All @@ -242,7 +248,7 @@ func TestHandleDocUpload_FileExist(t *testing.T) {
cc.Request.Header.Add("Content-Type", writer.FormDataContentType())

// second handling
HandleDocUpload(cc)
docHandler.HandleDocUpload(c)

// second statement
require.Equal(t, http.StatusConflict, ww.Result().StatusCode)
Expand Down
5 changes: 2 additions & 3 deletions src/handlers/docs/handleDocsRename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/util"
"github.com/kevinanielsen/go-fast-cdn/src/validations"
)

func HandleDocsRename(c *gin.Context) {
func (h *DocHandler) HandleDocsRename(c *gin.Context) {
oldName := c.PostForm("filename")
newName := c.PostForm("newname")

Expand All @@ -31,7 +30,7 @@ func HandleDocsRename(c *gin.Context) {
return
}

err = database.RenameDoc(oldName, newName)
err = h.repo.RenameDoc(oldName, newName)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error())
return
Expand Down
11 changes: 11 additions & 0 deletions src/handlers/image/ImageHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package handlers

import "github.com/kevinanielsen/go-fast-cdn/src/models"

type ImageHandler struct {
repo models.ImageRepository
}

func NewImageHandler(repo models.ImageRepository) *ImageHandler {
return &ImageHandler{repo}
}
7 changes: 2 additions & 5 deletions src/handlers/image/handleAllImages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/models"
)

func HandleAllImages(c *gin.Context) {
var entries []models.Image
func (h *ImageHandler) HandleAllImages(c *gin.Context) {

database.DB.Find(&entries, &models.Image{})
entries := h.repo.GetAllImages()

c.JSON(http.StatusOK, entries)
}
5 changes: 2 additions & 3 deletions src/handlers/image/handleImageDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/util"
)

func HandleImageDelete(c *gin.Context) {
func (h *ImageHandler) HandleImageDelete(c *gin.Context) {
fileName := c.Param("filename")
if fileName == "" {
c.JSON(http.StatusBadRequest, gin.H{
Expand All @@ -17,7 +16,7 @@ func HandleImageDelete(c *gin.Context) {
return
}

deletedFileName, success := database.DeleteImage(fileName)
deletedFileName, success := h.repo.DeleteImage(fileName)
if !success {
c.JSON(http.StatusNotFound, gin.H{
"error": "Image not found",
Expand Down
5 changes: 2 additions & 3 deletions src/handlers/image/handleImageRename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kevinanielsen/go-fast-cdn/src/database"
"github.com/kevinanielsen/go-fast-cdn/src/util"
"github.com/kevinanielsen/go-fast-cdn/src/validations"
)

func HandleImageRename(c *gin.Context) {
func (h *ImageHandler) HandleImageRename(c *gin.Context) {
oldName := c.PostForm("filename")
newName := c.PostForm("newname")

Expand All @@ -31,7 +30,7 @@ func HandleImageRename(c *gin.Context) {
return
}

err = database.RenameImage(oldName, filteredNewName)
err = h.repo.RenameImage(oldName, filteredNewName)
if err != nil {
c.String(http.StatusInternalServerError, "Failed to rename file: %s", err.Error())
return
Expand Down
Loading

0 comments on commit f1e2e42

Please sign in to comment.