Skip to content

Commit

Permalink
feat(collection): new api DeleteSubjectCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed May 14, 2024
1 parent d085c24 commit f67e767
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/collections/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type Repo interface {
update func(ctx context.Context, s *collection.Subject) (*collection.Subject, error),
) error

DeleteSubjectCollection(
ctx context.Context, userID model.UserID, subjectID model.SubjectID,
) error

UpdateEpisodeCollection(
ctx context.Context,
userID model.UserID, subjectID model.SubjectID,
Expand Down
11 changes: 11 additions & 0 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ func (r mysqlRepo) UpdateOrCreateSubjectCollection(
return r.updateOrCreateSubjectCollection(ctx, userID, subject, at, ip, update, s)
}

func (r mysqlRepo) DeleteSubjectCollection(
ctx context.Context,
userID model.UserID,
subjectID model.SubjectID,
) error {
_, err := r.q.SubjectCollection.WithContext(ctx).
Where(r.q.SubjectCollection.UserID.Eq(userID), r.q.SubjectCollection.SubjectID.Eq(subjectID)).
Delete()
return err
}

func (r mysqlRepo) updateOrCreateSubjectCollection(
ctx context.Context,
userID model.UserID,
Expand Down
33 changes: 33 additions & 0 deletions internal/collections/infra/mysql_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/trim21/go-phpserialize"
"go.uber.org/zap"
"gorm.io/gen/field"
"gorm.io/gorm"

"github.com/bangumi/server/dal/dao"
"github.com/bangumi/server/dal/query"
Expand Down Expand Up @@ -415,6 +416,38 @@ func TestMysqlRepo_UpdateSubjectCollectionType(t *testing.T) {
require.Zero(t, r.DoneTime)
require.Zero(t, r.OnHoldTime)
}

func TestMysqlRepo_DeleteSubjectCollection(t *testing.T) {
t.Parallel()
test.RequireEnv(t, test.EnvMysql)

const id model.UserID = 30000
const subjectID model.SubjectID = 10000

repo, q := getRepo(t)

test.RunAndCleanup(t, func() {
_, err := q.WithContext(context.Background()).SubjectCollection.
Where(q.SubjectCollection.SubjectID.Eq(subjectID), q.SubjectCollection.UserID.Eq(id)).Delete()
require.NoError(t, err)
})

err := q.WithContext(context.Background()).SubjectCollection.Create(&dao.SubjectCollection{
UserID: id,
SubjectID: subjectID,
Rate: 2,
})
require.NoError(t, err)

err = repo.DeleteSubjectCollection(context.Background(), id, subjectID)
require.NoError(t, err)

r, err := q.WithContext(context.Background()).SubjectCollection.
Where(q.SubjectCollection.SubjectID.Eq(subjectID), q.SubjectCollection.UserID.Eq(id)).Take()
require.ErrorIs(t, err, gorm.ErrRecordNotFound)
require.Nil(t, r)
}

func TestMysqlRepo_UpdateEpisodeCollection(t *testing.T) {
test.RequireEnv(t, test.EnvMysql)
t.Parallel()
Expand Down
48 changes: 48 additions & 0 deletions internal/mocks/CollectionRepo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions openapi/v0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,40 @@ paths:
"$ref": "#/components/schemas/ErrorDetail"
security:
- OptionalHTTPBearer: []
delete:
tags:
- 收藏
summary: 删除用户单个收藏
description: |
删除条目收藏状态
会删除当前填写的评分、tags 和吐槽,不会影响章节进度
operationId: deleteUserCollection
parameters:
- $ref: "#/components/parameters/path_subject_id"
responses:
"204":
description: Successful Response
"400":
description: Validation Error
content:
application/json:
schema:
"$ref": "#/components/schemas/ErrorDetail"
"401":
description: Unauthorized
content:
application/json:
schema:
"$ref": "#/components/schemas/ErrorDetail"
"404":
description: 用户不存在或者条目未收藏
content:
application/json:
schema:
"$ref": "#/components/schemas/ErrorDetail"
security:
- OptionalHTTPBearer: []

"/v0/users/-/collections/{subject_id}/episodes":
get:
Expand Down
53 changes: 53 additions & 0 deletions web/handler/user/delete_subject_collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>

package user

import (
"errors"
"net/http"

"github.com/labstack/echo/v4"

"github.com/bangumi/server/domain/gerr"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/web/accessor"
"github.com/bangumi/server/web/req"
"github.com/bangumi/server/web/res"
)

func (h User) DeleteSubjectCollection(c echo.Context) error {
subjectID, err := req.ParseID(c.Param("subject_id"))
if err != nil {
return err

Check warning on line 33 in web/handler/user/delete_subject_collection.go

View check run for this annotation

Codecov / codecov/patch

web/handler/user/delete_subject_collection.go#L33

Added line #L33 was not covered by tests
}

return h.deleteSubjectCollection(c, subjectID)
}

func (h User) deleteSubjectCollection(c echo.Context, subjectID model.SubjectID) error {
u := accessor.GetFromCtx(c)

err := h.collect.DeleteSubjectCollection(c.Request().Context(), u.ID, subjectID)
if err != nil {
switch {
case errors.Is(err, gerr.ErrSubjectNotCollected):
return res.JSONError(c, err)
default:
return err

Check warning on line 48 in web/handler/user/delete_subject_collection.go

View check run for this annotation

Codecov / codecov/patch

web/handler/user/delete_subject_collection.go#L44-L48

Added lines #L44 - L48 were not covered by tests
}
}

return c.NoContent(http.StatusNoContent)
}
50 changes: 50 additions & 0 deletions web/handler/user/delete_subject_collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>

package user_test

import (
"fmt"
"net/http"
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/mock"
"github.com/trim21/htest"

"github.com/bangumi/server/internal/auth"
"github.com/bangumi/server/internal/mocks"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/test"
)

func TestUser_DeleteSubjectCollection(t *testing.T) {
t.Parallel()
const uid model.UserID = 1
const sid model.SubjectID = 8

a := mocks.NewAuthService(t)
a.EXPECT().GetByToken(mock.Anything, mock.Anything).Return(auth.Auth{ID: uid}, nil)

c := mocks.NewCollectionRepo(t)
c.EXPECT().DeleteSubjectCollection(mock.Anything, uid, sid).
Return(nil)

app := test.GetWebApp(t, test.Mock{CollectionRepo: c, AuthService: a})

htest.New(t, app).
Header(echo.HeaderAuthorization, "Bearer t").
Delete(fmt.Sprintf("/v0/users/-/collections/%d", sid)).
ExpectCode(http.StatusNoContent)
}
1 change: 1 addition & 0 deletions web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func AddRouters(
v0.GET("/users/-/collections/:subject_id/episodes", userHandler.GetSubjectEpisodeCollection, mw.NeedLogin)
v0.PATCH("/users/-/collections/:subject_id", userHandler.PatchSubjectCollection, req.JSON, mw.NeedLogin)
v0.POST("/users/-/collections/:subject_id", userHandler.PostSubjectCollection, req.JSON, mw.NeedLogin)
v0.DELETE("/users/-/collections/:subject_id", userHandler.DeleteSubjectCollection, mw.NeedLogin)
v0.PATCH("/users/-/collections/:subject_id/episodes",
userHandler.PatchEpisodeCollectionBatch, req.JSON, mw.NeedLogin)

Expand Down

0 comments on commit f67e767

Please sign in to comment.