Skip to content

Commit 54d42ca

Browse files
committed
Implement puzzle deletion and theme management; enhance logging in theme creation
1 parent d3f37f8 commit 54d42ca

3 files changed

Lines changed: 77 additions & 46 deletions

File tree

controllers/puzzles.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,25 @@ func (p *PuzzleController) DeletePuzzle(c *gin.Context) {
224224
return
225225
}
226226

227-
// TODO: Implement puzzle deletion
228-
// This would remove the puzzle directory and .alghive file
227+
// Delete the puzzle opened directory if it exists, and the .alghive file
228+
puzzleDir := filepath.Join(services.PuzzlesDir, themeName, puzzleName)
229+
if err := services.RemoveAll(puzzleDir); err != nil {
230+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete puzzle directory"})
231+
return
232+
}
233+
alghiveFile := filepath.Join(services.PuzzlesDir, themeName, puzzleName+".alghive")
234+
if err := services.RemoveAll(alghiveFile); err != nil {
235+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete puzzle file"})
236+
return
237+
}
238+
239+
// Remove puzzle from theme
240+
for i, puzzle := range theme.Puzzles {
241+
if puzzle.GetName() == puzzleName {
242+
theme.Puzzles = append(theme.Puzzles[:i], theme.Puzzles[i+1:]...)
243+
break
244+
}
245+
}
229246

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

services/loader.go

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package services
33
import (
44
"archive/zip"
55
"io"
6+
"log"
67
"os"
78
"path/filepath"
89
"sync"
@@ -163,54 +164,62 @@ func (p *PuzzlesLoader) HasTheme(name string) bool {
163164
return p.GetTheme(name) != nil
164165
}
165166

166-
// CreateTheme creates a new theme
167167
func (p *PuzzlesLoader) CreateTheme(name string) error {
168-
p.mu.Lock()
169-
defer p.mu.Unlock()
170-
171-
if p.HasTheme(name) {
172-
return os.ErrExist
173-
}
174-
175-
themePath := filepath.Join(PuzzlesDir, name)
176-
err := os.MkdirAll(themePath, 0755)
177-
if err != nil {
178-
return err
179-
}
180-
181-
p.Themes = append(p.Themes, models.Theme{
182-
Name: name,
183-
Path: themePath,
184-
Puzzles: []models.Puzzle{},
185-
})
186-
187-
return nil
168+
log.Printf("Creating theme: %s", name)
169+
170+
p.mu.Lock()
171+
defer p.mu.Unlock()
172+
173+
// Avoid calling HasTheme here to prevent a lock conflict
174+
for _, theme := range p.Themes {
175+
if theme.Name == name {
176+
return os.ErrExist
177+
}
178+
}
179+
180+
themePath := filepath.Join(PuzzlesDir, name)
181+
err := os.MkdirAll(themePath, 0755)
182+
if err != nil {
183+
return err
184+
}
185+
186+
p.Themes = append(p.Themes, models.Theme{
187+
Name: name,
188+
Path: themePath,
189+
Puzzles: []models.Puzzle{},
190+
})
191+
192+
return nil
188193
}
189194

190-
// DeleteTheme deletes a theme
191195
func (p *PuzzlesLoader) DeleteTheme(name string) error {
192-
p.mu.Lock()
193-
defer p.mu.Unlock()
194-
195-
theme := p.GetTheme(name)
196-
if theme == nil {
197-
return os.ErrNotExist
198-
}
199-
200-
err := os.RemoveAll(theme.Path)
201-
if err != nil {
202-
return err
203-
}
204-
205-
// Remove theme from slice
206-
for i, t := range p.Themes {
207-
if t.Name == name {
208-
p.Themes = append(p.Themes[:i], p.Themes[i+1:]...)
209-
break
210-
}
211-
}
212-
213-
return nil
196+
p.mu.Lock()
197+
defer p.mu.Unlock()
198+
199+
idx := -1
200+
var themePath string
201+
202+
for i, theme := range p.Themes {
203+
if theme.Name == name {
204+
idx = i
205+
themePath = theme.Path
206+
break
207+
}
208+
}
209+
210+
if idx == -1 {
211+
return os.ErrNotExist
212+
}
213+
214+
err := os.RemoveAll(themePath)
215+
if err != nil {
216+
return err
217+
}
218+
219+
// Remove theme from slice
220+
p.Themes = append(p.Themes[:idx], p.Themes[idx+1:]...)
221+
222+
return nil
214223
}
215224

216225
// GetPuzzleSizes returns the compressed and uncompressed sizes of a puzzle

services/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ func GetDirSize(path string) (int64, error) {
1919
})
2020
return size, err
2121
}
22+
23+
// RemoveAll removes a directory and all its contents
24+
func RemoveAll(path string) error {
25+
return os.RemoveAll(path)
26+
}

0 commit comments

Comments
 (0)