|
| 1 | +package logic |
| 2 | + |
| 3 | +import ( |
| 4 | + "ch/kirari04/videocms/helpers" |
| 5 | + "ch/kirari04/videocms/inits" |
| 6 | + "ch/kirari04/videocms/models" |
| 7 | + "errors" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +func MoveItems(userId uint, targetFolderId uint, folderIds []uint, linkIds []uint) (int, error) { |
| 12 | + // check if at least one item is being moved |
| 13 | + if len(folderIds) == 0 && len(linkIds) == 0 { |
| 14 | + return http.StatusBadRequest, errors.New("no items selected to move") |
| 15 | + } |
| 16 | + |
| 17 | + // 1. Validate Target Folder |
| 18 | + if targetFolderId > 0 { |
| 19 | + var targetFolder models.Folder |
| 20 | + if res := inits.DB.First(&targetFolder, targetFolderId); res.Error != nil { |
| 21 | + return http.StatusBadRequest, errors.New("target folder doesn't exist") |
| 22 | + } |
| 23 | + if targetFolder.UserID != userId { |
| 24 | + return http.StatusForbidden, errors.New("unauthorized access to target folder") |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // 2. Move Folders |
| 29 | + for _, folderId := range folderIds { |
| 30 | + // Cannot move a folder into itself |
| 31 | + if folderId == targetFolderId { |
| 32 | + return http.StatusBadRequest, errors.New("cannot move folder into itself") |
| 33 | + } |
| 34 | + |
| 35 | + var folder models.Folder |
| 36 | + if res := inits.DB.First(&folder, folderId); res.Error != nil { |
| 37 | + return http.StatusBadRequest, errors.New("folder to move not found") |
| 38 | + } |
| 39 | + |
| 40 | + if folder.UserID != userId { |
| 41 | + return http.StatusForbidden, errors.New("unauthorized access to folder") |
| 42 | + } |
| 43 | + |
| 44 | + // Loop detection: Check if the folder we are moving contains the target folder |
| 45 | + if targetFolderId > 0 { |
| 46 | + contains, err := helpers.FolderContainsFolder(folderId, targetFolderId) |
| 47 | + if err != nil { |
| 48 | + return http.StatusInternalServerError, err |
| 49 | + } |
| 50 | + if contains { |
| 51 | + return http.StatusBadRequest, errors.New("cannot move parent folder into its child") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + folder.ParentFolderID = targetFolderId |
| 56 | + if res := inits.DB.Save(&folder); res.Error != nil { |
| 57 | + return http.StatusInternalServerError, res.Error |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // 3. Move Links (Files) |
| 62 | + for _, linkId := range linkIds { |
| 63 | + var link models.Link |
| 64 | + if res := inits.DB.First(&link, linkId); res.Error != nil { |
| 65 | + return http.StatusBadRequest, errors.New("file to move not found") |
| 66 | + } |
| 67 | + |
| 68 | + if link.UserID != userId { |
| 69 | + return http.StatusForbidden, errors.New("unauthorized access to file") |
| 70 | + } |
| 71 | + |
| 72 | + link.ParentFolderID = targetFolderId |
| 73 | + if res := inits.DB.Save(&link); res.Error != nil { |
| 74 | + return http.StatusInternalServerError, res.Error |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return http.StatusOK, nil |
| 79 | +} |
0 commit comments