From 4a5841b58bb5b03ee90930355d7c182c45d5834d Mon Sep 17 00:00:00 2001 From: Sam Ezeh Date: Fri, 23 Dec 2022 00:44:30 +0000 Subject: [PATCH 1/3] Allow study search results to be ordered --- app/controllers/Study.scala | 36 ++++++++++++++---------- app/views/study/list.scala | 7 +++-- conf/routes | 3 +- modules/studySearch/src/main/Query.scala | 1 + 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/controllers/Study.scala b/app/controllers/Study.scala index 93c9fc37df06..95981a268cc1 100644 --- a/app/controllers/Study.scala +++ b/app/controllers/Study.scala @@ -25,22 +25,27 @@ final class Study( prismicC: Prismic ) extends LilaController(env): - def search(text: String, page: Int) = + def search(text: String, order: String, page: Int) = OpenBody { implicit ctx => Reasonable(page) { - if (text.trim.isEmpty) - env.study.pager.all(ctx.me, Order.default, page) flatMap { pag => - preloadMembers(pag) >> negotiate( - html = Ok(html.study.list.all(pag, Order.default)).toFuccess, - api = _ => apiStudies(pag) - ) - } - else - env.studySearch(ctx.me)(text, page) flatMap { pag => - negotiate( - html = Ok(html.study.list.search(pag, text)).toFuccess, - api = _ => apiStudies(pag) - ) + Order(order) match + case order if !Order.withoutSelector.contains(order) => + Redirect(routes.Study.searchDefault(text, page)).toFuccess + case order => { + if (text.trim.isEmpty) + env.study.pager.all(ctx.me, order, page) flatMap { pag => + preloadMembers(pag) >> negotiate( + html = Ok(html.study.list.all(pag, order)).toFuccess, + api = _ => apiStudies(pag) + ) + } + else + env.studySearch(ctx.me)(text, page) flatMap { pag => + negotiate( + html = Ok(html.study.list.search(pag, order, text)).toFuccess, + api = _ => apiStudies(pag) + ) + } } } } @@ -48,7 +53,8 @@ final class Study( def homeLang = LangPage(routes.Study.allDefault())(allResults(Order.Hot.key, 1)(_)) - def allDefault(page: Int) = all(Order.Hot.key, page) + def allDefault(page: Int) = all(Order.Hot.key, page) + def searchDefault(text: String, page: Int) = search(text, Order.Hot.key, page) def all(o: String, page: Int) = Open { implicit ctx => diff --git a/app/views/study/list.scala b/app/views/study/list.scala index 896997f64874..bc0188a7fb06 100644 --- a/app/views/study/list.scala +++ b/app/views/study/list.scala @@ -95,7 +95,7 @@ object list: url = o => routes.Study.minePrivate(o) ) - def search(pag: Paginator[WithChaptersAndLiked], text: String)(implicit ctx: Context) = + def search(pag: Paginator[WithChaptersAndLiked], order: Order, text: String)(implicit ctx: Context) = views.html.base.layout( title = text, moreCss = cssTag("study.index"), @@ -107,9 +107,10 @@ object list: main(cls := "page-menu__content study-index box")( div(cls := "box__top")( searchForm(trans.search.search.txt(), text), + bits.orderSelect(order, "", url = o => routes.Study.search(text, o, 1)), bits.newForm() ), - paginate(pag, routes.Study.search(text)) + paginate(pag, routes.Study.searchDefault(text)) ) ) } @@ -163,7 +164,7 @@ object list: ) private[study] def searchForm(placeholder: String, value: String) = - form(cls := "search", action := routes.Study.search(), method := "get")( + form(cls := "search", action := routes.Study.searchDefault(), method := "get")( input(name := "q", st.placeholder := placeholder, st.value := value), submitButton(cls := "button", dataIcon := "") ) diff --git a/conf/routes b/conf/routes index 8103abd4265e..5b6690765083 100644 --- a/conf/routes +++ b/conf/routes @@ -203,7 +203,8 @@ GET /study/likes/:order controllers.Study.mineLikes(order, page: GET /study/by/:username controllers.Study.byOwnerDefault(username, page: Int ?= 1) GET /study/by/:username/export.pgn controllers.Study.exportPgn(username) GET /study/by/:username/:order controllers.Study.byOwner(username, order, page: Int ?= 1) -GET /study/search controllers.Study.search(q ?= "", page: Int ?= 1) +GET /study/search controllers.Study.searchDefault(q ?= "", page: Int ?= 1) +GET /study/search/:order controllers.Study.search(q ?= "", order, page: Int ?= 1) GET /study/$id<\w{8}> controllers.Study.show(id) POST /study controllers.Study.create POST /study/as controllers.Study.createAs diff --git a/modules/studySearch/src/main/Query.scala b/modules/studySearch/src/main/Query.scala index a6ffd1995923..287e20237dc0 100644 --- a/modules/studySearch/src/main/Query.scala +++ b/modules/studySearch/src/main/Query.scala @@ -3,6 +3,7 @@ package lila.studySearch import lila.user.User import play.api.libs.json.* import lila.common.Json.given +import lila.study.Order private[studySearch] case class Query(text: String, userId: Option[UserId]) From ab41b05508e01fc0c83cec382117efc156ff3696 Mon Sep 17 00:00:00 2001 From: Sam Ezeh Date: Fri, 23 Dec 2022 00:44:57 +0000 Subject: [PATCH 2/3] Format Scala --- app/views/study/list.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/study/list.scala b/app/views/study/list.scala index bc0188a7fb06..99ef6b08f1f8 100644 --- a/app/views/study/list.scala +++ b/app/views/study/list.scala @@ -165,7 +165,7 @@ object list: private[study] def searchForm(placeholder: String, value: String) = form(cls := "search", action := routes.Study.searchDefault(), method := "get")( - input(name := "q", st.placeholder := placeholder, st.value := value), + input(name := "q", st.placeholder := placeholder, st.value := value), submitButton(cls := "button", dataIcon := "") ) From 613f2cb84bcb38251bd411a3e5407e9ffc52d165 Mon Sep 17 00:00:00 2001 From: Sam Ezeh Date: Fri, 23 Dec 2022 20:35:39 +0000 Subject: [PATCH 3/3] Preserve display order --- app/views/study/list.scala | 2 +- modules/studySearch/src/main/Query.scala | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/study/list.scala b/app/views/study/list.scala index 99ef6b08f1f8..500fe68b2972 100644 --- a/app/views/study/list.scala +++ b/app/views/study/list.scala @@ -110,7 +110,7 @@ object list: bits.orderSelect(order, "", url = o => routes.Study.search(text, o, 1)), bits.newForm() ), - paginate(pag, routes.Study.searchDefault(text)) + paginate(pag, routes.Study.search(text, order.key)) ) ) } diff --git a/modules/studySearch/src/main/Query.scala b/modules/studySearch/src/main/Query.scala index 287e20237dc0..a6ffd1995923 100644 --- a/modules/studySearch/src/main/Query.scala +++ b/modules/studySearch/src/main/Query.scala @@ -3,7 +3,6 @@ package lila.studySearch import lila.user.User import play.api.libs.json.* import lila.common.Json.given -import lila.study.Order private[studySearch] case class Query(text: String, userId: Option[UserId])