Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
feat(web): added new endpoint DELETE /api/v1/audio - remove audio by id
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Apr 12, 2024
1 parent 594ec48 commit 2eb1b64
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
34 changes: 34 additions & 0 deletions web/internal/audio/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package audio

import (
"context"
"errors"
"github.com/wittano/komputer/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"os"
)

const audioCollectionName = "audio"
Expand All @@ -13,6 +15,8 @@ type DatabaseService struct {
Database db.MongodbService
}

var NotFoundErr = errors.New("audio not found")

func (a DatabaseService) save(ctx context.Context, filename string) error {
client, err := a.Database.Client(ctx)
if err != nil {
Expand Down Expand Up @@ -46,3 +50,33 @@ func (a DatabaseService) Get(ctx context.Context, id string) (result db.AudioInf

return
}

func (a DatabaseService) Delete(ctx context.Context, id string) (err error) {
hex, err := primitive.ObjectIDFromHex(id)
if err != nil {
return
}

info, err := a.Get(ctx, id)
if err != nil {
return errors.Join(NotFoundErr, err)
}

client, err := a.Database.Client(ctx)
if err != nil {
return
}

_, err = client.Database(db.DatabaseName).
Collection(audioCollectionName).
DeleteOne(ctx, bson.D{{"_id", hex}})
if err != nil {
return
}

if _, err = os.Stat(info.Path); errors.Is(err, os.ErrNotExist) {
return os.Remove(info.Path)
}

return
}
20 changes: 17 additions & 3 deletions web/internal/handler/audio.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import (
"errors"
"fmt"
"github.com/labstack/echo/v4"
"github.com/wittano/komputer/db"
"github.com/wittano/komputer/web/internal/audio"
Expand All @@ -10,19 +12,31 @@ import (
)

func GetAudio(c echo.Context) error {
id := c.Param("id")

ctx := c.Request().Context()
service := audio.DatabaseService{Database: db.Mongodb(ctx)}

info, err := service.Get(ctx, id)
info, err := service.Get(ctx, c.Param("id"))
if err != nil {
return err
}

return c.File(info.Path)
}

func RemoveAudio(c echo.Context) error {
ctx := c.Request().Context
service := audio.DatabaseService{Database: db.Mongodb(ctx())}

id := c.Param("id")
if err := service.Delete(ctx(), id); errors.Is(err, audio.NotFoundErr) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("audio with id '%s' wasn't found", id))
} else if err != nil {
return err
}

return c.String(http.StatusOK, "")
}

func UploadNewAudio(c echo.Context) (err error) {
multipartForm, err := c.MultipartForm()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewWebConsoleServer(configPath string) (*echo.Echo, error) {

e.POST("/api/v1/audio", handler.UploadNewAudio)
e.GET("/api/v1/audio/:id", handler.GetAudio)
e.DELETE("/api/v1/audio/:id", handler.RemoveAudio)

e.GET("/api/v1/setting", handler.GetSettings)
e.PUT("/api/v1/setting", handler.UpdateSettings)
Expand Down

0 comments on commit 2eb1b64

Please sign in to comment.