Skip to content

Commit

Permalink
Gofumpted
Browse files Browse the repository at this point in the history
  • Loading branch information
SebiWrn committed Sep 21, 2024
1 parent 4abede8 commit 1c10aa5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
23 changes: 12 additions & 11 deletions api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/tools"
"github.com/gin-gonic/gin"
"github.com/meilisearch/meilisearch-go"
"math"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/tools"
"github.com/gin-gonic/gin"
"github.com/meilisearch/meilisearch-go"
)

func configGinSearchRouter(router *gin.Engine, daoWrapper dao.DaoWrapper, meiliSearchInstance tools.MeiliSearchInterface) {
Expand Down Expand Up @@ -91,7 +92,7 @@ func getDefaultParameters(c *gin.Context) (*model.User, string, uint64) {
user := c.MustGet("TUMLiveContext").(tools.TUMLiveContext).User
query := c.Query("q")
limit, err := strconv.ParseUint(c.Query("limit"), 10, 16)
if err != nil || limit > math.MaxInt64 { //second condition should never happen, max bitSize for parseuint is 16
if err != nil || limit > math.MaxInt64 { // second condition should never happen, max bitSize for parseuint is 16
limit = DefaultLimit
}
return user, query, limit
Expand Down Expand Up @@ -247,7 +248,7 @@ func checkAndFillResponse(c *gin.Context, user *model.User, limit int64, daoWrap

var meiliStreams []SearchStreamDTO
temp, err := json.Marshal(hits)
if err != nil { //shouldn't happen
if err != nil { // shouldn't happen
continue
}
err = json.Unmarshal(temp, &meiliStreams)
Expand Down Expand Up @@ -282,7 +283,7 @@ func checkAndFillResponse(c *gin.Context, user *model.User, limit int64, daoWrap

var meiliCourses []SearchCourseDTO
temp, err := json.Marshal(hits)
if err != nil { //shouldn't happen
if err != nil { // shouldn't happen
continue
}
err = json.Unmarshal(temp, &meiliCourses)
Expand All @@ -308,7 +309,7 @@ func checkAndFillResponse(c *gin.Context, user *model.User, limit int64, daoWrap

var meiliSubtitles []SearchSubtitlesDTO
temp, err := json.Marshal(hits)
if err != nil { //shouldn't happen
if err != nil { // shouldn't happen
continue
}
err = json.Unmarshal(temp, &meiliSubtitles)
Expand Down Expand Up @@ -433,12 +434,12 @@ func meiliSemesterFilter(firstSemester model.Semester, lastSemester model.Semest
}

if semesters == nil {
//single semester
// single semester
if firstSemester.Year == lastSemester.Year && firstSemester.TeachingTerm == lastSemester.TeachingTerm {
return fmt.Sprintf("(year = %d AND semester = \"%s\")", firstSemester.Year, firstSemester.TeachingTerm)
}

//multiple semesters
// multiple semesters
var constraint1, constraint2 string
if firstSemester.TeachingTerm == "W" {
constraint1 = fmt.Sprintf("(year = %d AND semester = \"%s\")", firstSemester.Year, firstSemester.TeachingTerm)
Expand Down
27 changes: 15 additions & 12 deletions api/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package api
import (
"errors"
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"testing"

"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/mock_dao"
"github.com/TUM-Dev/gocast/mock_tools"
Expand All @@ -13,16 +19,9 @@ import (
"github.com/golang/mock/gomock"
"github.com/matthiasreumann/gomino"
"github.com/meilisearch/meilisearch-go"
"net/http"
"slices"
"strconv"
"strings"
"testing"
)

var (
emptySlice = []any{}
)
var emptySlice = []any{}

Check failure on line 24 in api/search_test.go

View workflow job for this annotation

GitHub Actions / lint (./...)

var `emptySlice` is unused (unused)

Check failure on line 24 in api/search_test.go

View workflow job for this annotation

GitHub Actions / lint (./worker)

var `emptySlice` is unused (unused)

Check failure on line 24 in api/search_test.go

View workflow job for this annotation

GitHub Actions / lint (./worker/edge)

var `emptySlice` is unused (unused)

func SearchRouterWrapper(r *gin.Engine) {
configGinSearchRouter(r, dao.DaoWrapper{}, tools.NewMeiliSearchFunctions())
Expand Down Expand Up @@ -356,13 +355,15 @@ func getMeiliSearchMock(t *testing.T, daoWrapper dao.DaoWrapper) *mock_tools.Moc
streams, _ := tools.ToMeiliStreams(testutils.AllStreamsForSearchTests, daoWrapper)
return &meilisearch.MultiSearchResponse{Results: []meilisearch.SearchResponse{
{IndexUID: "COURSES", Hits: meiliCourseSliceToInterfaceSlice(tools.ToMeiliCourses(testutils.AllCoursesForSearchTests))},
{IndexUID: "STREAMS", Hits: meiliStreamSliceToInterfaceSlice(streams)}}}
{IndexUID: "STREAMS", Hits: meiliStreamSliceToInterfaceSlice(streams)},
}}
}).AnyTimes()

mock.EXPECT().Search(gomock.Any(), gomock.Any(), 4, gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(
func(q interface{}, limit interface{}, searchType interface{}, courseFilter string, streamFilter string, subtitleFilter string) *meilisearch.MultiSearchResponse {
return &meilisearch.MultiSearchResponse{Results: []meilisearch.SearchResponse{
{IndexUID: "COURSES", Hits: meiliCourseSliceToInterfaceSlice(tools.ToMeiliCourses(testutils.AllCoursesForSearchTests))}}}
{IndexUID: "COURSES", Hits: meiliCourseSliceToInterfaceSlice(tools.ToMeiliCourses(testutils.AllCoursesForSearchTests))},
}}
}).AnyTimes()

mock.EXPECT().Search(gomock.Any(), gomock.Any(), 3, gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(
Expand All @@ -388,7 +389,8 @@ func getMeiliSearchMock(t *testing.T, daoWrapper dao.DaoWrapper) *mock_tools.Moc
returnStreams, _ := tools.ToMeiliStreams(streams, daoWrapper)
return &meilisearch.MultiSearchResponse{Results: []meilisearch.SearchResponse{
{IndexUID: "STREAMS", Hits: meiliStreamSliceToInterfaceSlice(returnStreams)},
{IndexUID: "SUBTITLES", Hits: meiliSubtitleSliceToInterfaceSlice(subtitles)}}}
{IndexUID: "SUBTITLES", Hits: meiliSubtitleSliceToInterfaceSlice(subtitles)},
}}
}).AnyTimes()
return mock
}
Expand All @@ -400,7 +402,8 @@ func getMeiliSearchMockReturningEveryStreamAndSubtitle(t *testing.T, daoWrapper
streams, _ := tools.ToMeiliStreams(testutils.AllStreamsForSearchTests, daoWrapper)
return &meilisearch.MultiSearchResponse{Results: []meilisearch.SearchResponse{
{IndexUID: "STREAMS", Hits: meiliStreamSliceToInterfaceSlice(streams)},
{IndexUID: "SUBTITLES", Hits: meiliSubtitleSliceToInterfaceSlice(testutils.AllSubtitlesForSearchTests)}}}
{IndexUID: "SUBTITLES", Hits: meiliSubtitleSliceToInterfaceSlice(testutils.AllSubtitlesForSearchTests)},
}}
}).AnyTimes()
return mock
}
Expand Down
5 changes: 3 additions & 2 deletions tools/meiliExporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/TUM-Dev/gocast/model"
"strings"

"github.com/TUM-Dev/gocast/model"

"github.com/TUM-Dev/gocast/dao"
"github.com/asticode/go-astisub"
"github.com/meilisearch/meilisearch-go"
Expand All @@ -21,7 +22,7 @@ type MeiliStream struct {
TeachingTerm string `json:"semester"`
CourseID uint `json:"courseID"`
Private uint `json:"private"`
Visibility string `json:"visibility"` //corresponds to the visibility of the course
Visibility string `json:"visibility"` // corresponds to the visibility of the course
}

type MeiliSubtitles struct {
Expand Down
4 changes: 2 additions & 2 deletions tools/meiliSearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tools

import (
"fmt"

"github.com/meilisearch/meilisearch-go"
)

Expand Down Expand Up @@ -90,7 +91,7 @@ func (d *meiliSearchFunctions) Search(q string, limit int64, searchType int, cou
bitOperator <<= 1
}

//multisearch Request
// multisearch Request
response, err := c.MultiSearch(&meilisearch.MultiSearchRequest{Queries: reqs})
if err != nil {
logger.Error("could not search in meili", "err", err)
Expand All @@ -110,7 +111,6 @@ func SearchCourses(q string, filter string) *meilisearch.SearchResponse {
Limit: 10,
AttributesToRetrieve: []string{"name", "slug", "year", "semester"},
})

if err != nil {
logger.Error("could not search courses in meili", "err", err)
return nil
Expand Down
8 changes: 4 additions & 4 deletions tools/testutils/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ var (
)

var (
//users
// users
LecturerNoCourse = model.User{
Model: gorm.Model{ID: 610},
Role: model.LecturerType,
Expand All @@ -349,7 +349,7 @@ var (
Courses: []model.Course{HiddenCourse, EnrolledCourse, LoggedinCourse, PublicCourse},
}

//courses
// courses
AllCoursesForSearchTests = []model.Course{HiddenCourse, EnrolledCourse, LoggedinCourse, PublicCourse}

HiddenCourse = model.Course{
Expand Down Expand Up @@ -397,7 +397,7 @@ var (
Admins: []model.User{},
}

//streans
// streans
AllStreamsForSearchTests = []model.Stream{StreamHiddenCourse, PrivateStreamHiddenCourse, StreamEnrolledCourse, PrivateStreamEnrolledCourse, StreamLoggedinCourse, PrivateStreamLoggedinCourse, StreamPublicCourse, PrivateStreamPublicCourse}

StreamHiddenCourse = model.Stream{
Expand Down Expand Up @@ -465,7 +465,7 @@ var (
Private: true,
}

//subtitles
// subtitles
AllSubtitlesForSearchTests = []tools.MeiliSubtitles{SubtitlesStreamHiddenCourse, SubtitlesPrivateStreamHiddenCourse, SubtitlesStreamEnrolledCourse, SubtitlesPrivateStreamEnrolledCourse, SubtitlesStreamLoggedinCourse, SubtitlesPrivateStreamLoggedinCourse, SubtitlesStreamPublicCourse, SubtitlesPrivateStreamPublicCourse}
SubtitlesStreamPublicCourse = tools.MeiliSubtitles{
ID: SubtitlesIDPublicCourseStream,
Expand Down

0 comments on commit 1c10aa5

Please sign in to comment.