Skip to content

Commit

Permalink
Merge pull request #67 from GenerateNU/feat/handle-file-dups
Browse files Browse the repository at this point in the history
Feat/handle file dups
  • Loading branch information
DOOduneye authored Dec 8, 2023
2 parents 2298141 + 6c8e668 commit 02a6e33
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 27 deletions.
1 change: 0 additions & 1 deletion client-new/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"react": "18.2.0",
"react-dom": "^18.2.0",
"react-native": "0.72.5",
"react-native-auth0": "^3.0.2",
"react-native-date-picker": "^4.3.3",
"react-native-document-picker": "^9.0.1",
"react-native-file-viewer": "^2.1.5",
Expand Down
2 changes: 1 addition & 1 deletion client-new/src/services/const.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* API_BASE_URL is a constant that is used to determine the base URL for the API.
*/
export const API_BASE_URL = true
export const API_BASE_URL = false
? 'http://localhost:8080/api'
: 'https://legacy.loca.lt/api';
2 changes: 1 addition & 1 deletion server/src/controllers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (f *FileController) CreateFile(c echo.Context) error {
func (f *FileController) DeleteFile(c echo.Context) error {
fileID := c.Param("fid")

if err := f.fileService.DeleteFile(fileID); err != nil {
if err := f.fileService.DeleteFile(fileID, false); err != nil {
return c.JSON(http.StatusNotFound, "Failed to delete file")
}

Expand Down
76 changes: 76 additions & 0 deletions server/src/controllers/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package controllers

import (
"net/http"
"server/src/services"

"github.com/labstack/echo/v4"
)

type ProgressController struct {
progressService services.ProgressServiceInterface
}

func NewProgressController(progressService services.ProgressServiceInterface) *ProgressController {
return &ProgressController{progressService: progressService}
}

func (p *ProgressController) GetTaskProgress(c echo.Context) error {
userID := c.Param("uid")
taskID := c.Param("tid")
taskProgress, err := p.progressService.GetTaskProgress(userID, taskID)

if err != nil {
return c.JSON(http.StatusNotFound, "failed to fetch task progress")
}

return c.JSON(http.StatusOK, taskProgress)
}

func (p *ProgressController) GetSubTaskProgress(c echo.Context) error {
userID := c.Param("uid")
subTaskID := c.Param("sid")
subTaskProgress, err := p.progressService.GetSubTaskProgress(userID, subTaskID)

if err != nil {
return c.JSON(http.StatusNotFound, "failed to fetch subtask progress")
}

return c.JSON(http.StatusOK, subTaskProgress)
}

func (p *ProgressController) CompleteTaskProgress(c echo.Context) error {
userID := c.Param("uid")
taskID := c.Param("tid")
taskProgress, err := p.progressService.CompleteTaskProgress(userID, taskID)

if err != nil {
return c.JSON(http.StatusNotFound, "failed to complete task progress")
}

return c.JSON(http.StatusOK, taskProgress)
}

func (p *ProgressController) CompleteSubTaskProgress(c echo.Context) error {
userID := c.Param("uid")
subTaskID := c.Param("sid")
subTaskProgress, err := p.progressService.CompleteSubTaskProgress(userID, subTaskID)

if err != nil {
return c.JSON(http.StatusNotFound, "failed to complete subtask progress")
}

return c.JSON(http.StatusOK, subTaskProgress)
}

func (p *ProgressController) GetAllSubTaskProgressOfTask(c echo.Context) error {
userID := c.Param("uid")
taskID := c.Param("tid")
subTaskProgress, err := p.progressService.GetAllSubTaskProgressOfTask(userID, taskID)

if err != nil {
return c.JSON(http.StatusNotFound, "failed to fetch all subtask progresses of task")
}

return c.JSON(http.StatusOK, subTaskProgress)
}
2 changes: 2 additions & 0 deletions server/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func AddRoutes(db *gorm.DB, e *echo.Echo) {
subTaskService := services.SubTaskService{DB: db}
fileService := services.FileService{DB: db}
guideService := services.GuideService{DB: db}
progressService := services.ProgressService{DB: db}

routes.UserRoutes(e.Group("/api/users"), &userService)
routes.ProfileRoutes(e.Group("/api/profiles"), &profileService)
Expand All @@ -54,6 +55,7 @@ func AddRoutes(db *gorm.DB, e *echo.Echo) {
routes.SubTaskRoutes(e.Group("/api/subtasks"), &subTaskService)
routes.FileRoutes(e.Group("/api/files"), &fileService)
routes.GuideRoutes(e.Group("/api/guides"), &guideService)
routes.ProgressRoutes(e.Group("/api/progresses"), &progressService)
}

func InitDB() (*gorm.DB, error) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/models/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type SubTaskProgress struct {
types.Model
Completed bool `gorm:"default:false" json:"completed"`
UserID uint `json:"user_id"`
SubTaskID uint `json:"subtask_id"`
SubTaskID uint `json:"sub_task_id"`
User *User `gorm:"foreignkey:UserID" json:"-"`
SubTask *SubTask `gorm:"foreignkey:SubTaskID" json:"-"`
}
19 changes: 19 additions & 0 deletions server/src/routes/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package routes

import (
"server/src/controllers"
"server/src/services"

"github.com/labstack/echo/v4"
)

func ProgressRoutes(g *echo.Group, progressService services.ProgressServiceInterface) {
progressController := controllers.NewProgressController(progressService)

g.GET("/task/:uid/:tid", progressController.GetTaskProgress)
g.GET("/subtask/:uid/:sid", progressController.GetSubTaskProgress)
g.GET("/:uid/:tid", progressController.GetAllSubTaskProgressOfTask)
g.POST("/task/:uid/:tid", progressController.CompleteTaskProgress)
g.POST("/subtask/:uid/:sid", progressController.CompleteSubTaskProgress)

}
46 changes: 37 additions & 9 deletions server/src/services/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/textproto"
"server/src/models"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -34,7 +35,7 @@ type FileServiceInterface interface {
GetFileURL(id string, days string) (string, error)
CreateFile(id string, file models.File, data *multipart.FileHeader, reader io.Reader) (models.File, error)
GeneratePDF(uid string, subtaskName string, fileJSON string) (*multipart.FileHeader, io.Reader, error)
DeleteFile(id string) error
DeleteFile(id string, s3Only bool) error
}

type FileService struct {
Expand Down Expand Up @@ -144,6 +145,7 @@ func (f *FileService) GetFileURL(id string, days string) (string, error) {

func (f *FileService) CreateFile(id string, file models.File, data *multipart.FileHeader, reader io.Reader) (models.File, error) {
var testFile models.File
var searchFiles []models.File
file.FileName = data.Filename
idInt, err := strconv.Atoi(id)
if err != nil {
Expand All @@ -152,14 +154,24 @@ func (f *FileService) CreateFile(id string, file models.File, data *multipart.Fi

file.UserID = uint(idInt)

// check if filename is already taken
// check if filename is already taken, and add (filenumber) to name if it is
objectKey := fmt.Sprintf("%v-%v", file.UserID, file.FileName)
if err := f.DB.Where("object_key = ?", objectKey).Find(&testFile).Error; err != nil {
return models.File{}, errors.New("file name already exists")
}
dotIndex := strings.LastIndex(objectKey, ".")
file_substring := objectKey[:dotIndex]
file_extension := objectKey[dotIndex:]
searchKey := file_substring + "%" + file_extension

file.ObjectKey = objectKey

if err := f.DB.Where("object_key = ?", objectKey).Find(&testFile).Error; err == nil {
f.DB.Where("object_key LIKE ?", searchKey).Find(&searchFiles)
i := len(searchFiles)

file_num := fmt.Sprintf(" (%v)", i)
file.ObjectKey = file_substring + file_num + file_extension
file.FileName = file_substring[strings.Index(file_substring, "-")+1:] + file_num + file_extension
}

// Check if the file size is greater than 5 MB
if data.Size > 5000000 {
return models.File{}, errors.New("maximum file size 5 MB")
Expand All @@ -186,6 +198,7 @@ func (f *FileService) CreateFile(id string, file models.File, data *multipart.Fi

// Create the file in the database
if err := f.DB.Create(&file).Error; err != nil {
f.DeleteFile(fmt.Sprint(file.ID), true) // delete file from s3 if it cant be made in database
return models.File{}, errors.New("failed to create file in database")
}

Expand Down Expand Up @@ -216,7 +229,20 @@ func (f *FileService) GeneratePDF(uid string, subtaskName string, fileJSON strin
}

default:
pdf.Text(x, y, fmt.Sprintf("%v: %v", key, value))
if key == "additional_comments" {
if stringValue, ok := value.(string); ok {
lines := pdf.SplitText("additional comments: "+stringValue, 170)
for _, words := range lines {
pdf.Text(x, y, words)
y += 6.0
}
y -= 6.0
} else {
return nil, nil, errors.New("unexpected input")
}
} else {
pdf.Text(x, y, fmt.Sprintf("%v: %v", key, value))
}
y += 10.0
}
}
Expand All @@ -237,7 +263,7 @@ func (f *FileService) GeneratePDF(uid string, subtaskName string, fileJSON strin
return fileHeader, reader, nil
}

func (f *FileService) DeleteFile(id string) error {
func (f *FileService) DeleteFile(id string, s3Only bool) error {
var file models.File

if err := f.DB.First(&file, id).Error; err != nil {
Expand All @@ -262,8 +288,10 @@ func (f *FileService) DeleteFile(id string) error {
}

// Required to delete the file from the database permanently
if err := f.DB.Unscoped().Delete(&file).Error; err != nil {
return err
if !s3Only {
if err := f.DB.Unscoped().Delete(&file).Error; err != nil {
return err
}
}

return nil
Expand Down
53 changes: 39 additions & 14 deletions server/src/services/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (
type ProgressServiceInterface interface {
GetAllTaskProgress() ([]models.TaskProgress, error)
GetAllSubTaskProgress() ([]models.SubTaskProgress, error)
GetAllSubTaskProgressOfTask(uid string, tid string) ([]models.SubTaskProgress, error)

GetTaskProgress(id string) (models.TaskProgress, error)
GetSubTaskProgress(id string) (models.SubTaskProgress, error)
GetTaskProgress(uid string, tid string) (models.TaskProgress, error)
GetSubTaskProgress(uid string, sid string) (models.SubTaskProgress, error)

CreateAllTaskProgress(id string) ([]models.TaskProgress, error)
CreateAllSubTaskProgress(id string) ([]models.SubTaskProgress, error)

CreateTaskProgress(taskProgress models.TaskProgress) (models.TaskProgress, error)
CreateSubTaskProgress(subTaskProgress models.SubTaskProgress) (models.SubTaskProgress, error)

CompleteTaskProgress(id string, taskProgress models.TaskProgress) (models.TaskProgress, error)
CompleteSubTaskProgress(id string, subTaskProgress models.SubTaskProgress) (models.SubTaskProgress, error)
CompleteTaskProgress(uid string, tid string) (models.TaskProgress, error)
CompleteSubTaskProgress(uid string, sid string) (models.SubTaskProgress, error)

DeleteTaskProgress(id string) error
DeleteSubTaskProgress(id string) error
Expand Down Expand Up @@ -52,20 +53,43 @@ func (p *ProgressService) GetAllSubTaskProgress() ([]models.SubTaskProgress, err
return subTaskProgress, nil
}

func (p *ProgressService) GetTaskProgress(id string) (models.TaskProgress, error) {
func (p *ProgressService) GetAllSubTaskProgressOfTask(uid string, tid string) ([]models.SubTaskProgress, error) {
var subTaskProgress []models.SubTaskProgress
var subTasks []models.SubTask
var subTaskIDs []uint

// find all subtasks associated with the task
if err := p.DB.Where("task_id = ?", tid).Find(&subTasks).Error; err != nil {
return []models.SubTaskProgress{}, err
}

// create slice of subtask ids
for _, subTask := range subTasks {
subTaskIDs = append(subTaskIDs, subTask.ID)
}

// fetch all the subtask progresses
if err := p.DB.Where("user_id = ? and sub_task_id IN ?", uid, subTaskIDs).Find(&subTaskProgress).Error; err != nil {
return nil, err
}

return subTaskProgress, nil
}

func (p *ProgressService) GetTaskProgress(uid string, tid string) (models.TaskProgress, error) {
var taskProgress models.TaskProgress

if err := p.DB.First(&taskProgress, id).Error; err != nil {
if err := p.DB.Where("user_id = ? and task_id = ?", uid, tid).Find(&taskProgress).Error; err != nil {
return models.TaskProgress{}, err
}

return taskProgress, nil
}

func (p *ProgressService) GetSubTaskProgress(id string) (models.SubTaskProgress, error) {
func (p *ProgressService) GetSubTaskProgress(uid string, sid string) (models.SubTaskProgress, error) {
var subTaskProgress models.SubTaskProgress

if err := p.DB.First(&subTaskProgress, id).Error; err != nil {
if err := p.DB.Where("user_id = ? and sub_task_id = ?", uid, sid).Find(&subTaskProgress).Error; err != nil {
return models.SubTaskProgress{}, err
}

Expand Down Expand Up @@ -158,28 +182,29 @@ func (p *ProgressService) CreateSubTaskProgress(subTaskProgress models.SubTaskPr
return subTaskProgress, nil
}

func (p *ProgressService) CompleteTaskProgress(id string, taskProgress models.TaskProgress) (models.TaskProgress, error) {
func (p *ProgressService) CompleteTaskProgress(uid string, tid string) (models.TaskProgress, error) {
var existingTaskProgress models.TaskProgress

if err := p.DB.First(&existingTaskProgress, id).Error; err != nil {
if err := p.DB.Model(&existingTaskProgress).Where("user_id = ? and task_id = ?", uid, tid).Update("completed", "true").Error; err != nil {
return models.TaskProgress{}, err
}

if err := p.DB.Model(&existingTaskProgress).Updates(taskProgress).Error; err != nil {
if err := p.DB.Where("user_id = ? and task_id = ?", uid, tid).Find(&existingTaskProgress).Error; err != nil {
return models.TaskProgress{}, err
}

return existingTaskProgress, nil

}

func (p *ProgressService) CompleteSubTaskProgress(id string, subTaskProgress models.SubTaskProgress) (models.SubTaskProgress, error) {
func (p *ProgressService) CompleteSubTaskProgress(uid string, sid string) (models.SubTaskProgress, error) {
var existingSubTaskProgress models.SubTaskProgress

if err := p.DB.First(&existingSubTaskProgress, id).Error; err != nil {
if err := p.DB.Model(&existingSubTaskProgress).Where("user_id = ? and sub_task_id = ?", uid, sid).Update("completed", "true").Error; err != nil {
return models.SubTaskProgress{}, err
}

if err := p.DB.Model(&existingSubTaskProgress).Updates(subTaskProgress).Error; err != nil {
if err := p.DB.Where("user_id = ? and sub_task_id = ?", uid, sid).Find(&existingSubTaskProgress).Error; err != nil {
return models.SubTaskProgress{}, err
}

Expand Down

0 comments on commit 02a6e33

Please sign in to comment.