This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search routes, controller, view, messages, and wiring
- Loading branch information
Showing
9 changed files
with
224 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package controllers | ||
|
||
import com.example.auction.search.api.{SearchRequest, SearchService} | ||
import com.example.auction.user.api.UserService | ||
import com.example.auction.utils.PaginatedSequence | ||
import com.typesafe.config.Config | ||
import play.api.data.Forms.{nonEmptyText, _} | ||
import play.api.data.{Form, Mapping} | ||
import play.api.mvc.{ControllerComponents, _} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class SearchController(config: Config, | ||
searchService: SearchService, | ||
userService: UserService, | ||
controllerComponents: ControllerComponents)(implicit ec: ExecutionContext) | ||
extends AbstractAuctionController(userService, controllerComponents) { | ||
|
||
private val showInlineInstruction: Boolean = config.getBoolean("online-auction.instruction.show") | ||
private val pageSize: Int = config.getInt("items-search.page-size") | ||
|
||
def searchForm(): Action[AnyContent] = Action.async { implicit request => | ||
requireUser(loadNav(_).map { implicit nav => | ||
Ok(views.html.searchItems(showInlineInstruction = showInlineInstruction, | ||
form = SearchItemsForm.form.fill(SearchItemsForm()), | ||
optionalSearchItemPaginatedSequence = None)) | ||
}) | ||
} | ||
|
||
def search(): Action[AnyContent] = Action.async { implicit request => | ||
requireUser(user => | ||
loadNav(user).flatMap { implicit nav => | ||
SearchItemsForm.form.bindFromRequest().fold( | ||
errorForm => { | ||
Future.successful(Ok(views.html.searchItems( | ||
showInlineInstruction = showInlineInstruction, | ||
form = errorForm, | ||
optionalSearchItemPaginatedSequence = None))) | ||
}, | ||
searchItemsForm => { | ||
searchService.search(searchItemsForm.pageNumber, pageSize) | ||
.invoke(SearchRequest( | ||
if (searchItemsForm.keywords.isEmpty) None else Some(searchItemsForm.keywords), | ||
Some(searchItemsForm.maximumPrice.intValue()), | ||
Some(searchItemsForm.currency.name))) | ||
.map(searchResponse => { | ||
Ok(views.html.searchItems( | ||
showInlineInstruction = showInlineInstruction, | ||
form = SearchItemsForm.form.fill(searchItemsForm), | ||
optionalSearchItemPaginatedSequence = Some(PaginatedSequence( | ||
searchResponse.items, | ||
searchResponse.pageNo, | ||
searchResponse.pageSize, | ||
searchResponse.numResults)))) | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
|
||
case class SearchItemsForm(keywords: String = "", | ||
maximumPrice: BigDecimal = 0.0, | ||
currency: Currency = Currency.USD, | ||
pageNumber: Int = 0) | ||
|
||
object SearchItemsForm { | ||
val currency: Mapping[Currency] = nonEmptyText | ||
.verifying("invalid.currency", c => Currency.isDefined(c)) | ||
.transform[Currency](Currency.valueOf, _.name) | ||
val form = Form(mapping( | ||
"keywords" -> text, | ||
"maximumPrice" -> bigDecimal, | ||
"currency" -> currency, | ||
"pageNumber" -> number(min = 0) | ||
)(SearchItemsForm.apply)(SearchItemsForm.unapply)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
@import com.example.auction.item.api.ItemStatus | ||
@import com.example.auction.item.api.ItemSummary | ||
@import com.example.auction.search.api.SearchItem | ||
@import com.example.auction.utils | ||
@import controllers.Currency | ||
@import helper._ | ||
@import controllers.{Currency => Currencies} | ||
|
||
@import java.util.Locale | ||
|
||
@import com.example.auction.utils.PaginatedSequence | ||
@(showInlineInstruction: Boolean, | ||
form: Form[SearchItemsForm], | ||
optionalSearchItemPaginatedSequence: Option[PaginatedSequence[SearchItem]] | ||
)(implicit nav: Nav, request: RequestHeader) | ||
|
||
@main(Messages("searchItems")) { | ||
|
||
<h2>@Messages("searchItems")</h2> | ||
|
||
@if(showInlineInstruction) { | ||
<p>@Messages("instruction.search")</p> | ||
|
||
<p>@Html(Messages("instruction.enableSearch", "<a href=\"https://github.com/lagom/online-auction-scala/blob/master/README.md\">README.md</a>"))</p> | ||
} | ||
|
||
@foundationForm(form, routes.SearchController.search()) { | ||
<div class="row"> | ||
<div class="medium-6 columns"> | ||
@inputText(form("keywords")) | ||
</div> | ||
</div> | ||
<div class="column row"> | ||
@inputText(form("maximumPrice"), 'type -> "number", 'min -> 0, 'value -> form("maximumPrice").value) | ||
@select( | ||
field = form("currency"), | ||
options = Currencies.values.map(c => c.name -> c.getDisplayName).sortBy(_._2) | ||
) | ||
</div> | ||
<input type="hidden" id="pageNumber" value="0" name="@form("pageNumber").name"/> | ||
|
||
<input class="button" type="submit" value="@Messages("searchItems")"/> | ||
} | ||
@if(optionalSearchItemPaginatedSequence.isDefined) { | ||
@defining(optionalSearchItemPaginatedSequence.get) { searchItemPaginatedSequence: PaginatedSequence[SearchItem] => | ||
|
||
@if(!searchItemPaginatedSequence.isEmpty) { | ||
<script> | ||
var updatePage = function (pageNum) { | ||
document.getElementById("pageNumber").value = pageNum; | ||
document.getElementById("searchForm").submit(); | ||
} | ||
</script> | ||
|
||
@if(searchItemPaginatedSequence.count < searchItemPaginatedSequence.pageSize) { | ||
<div>@Messages("searchFoundCount", searchItemPaginatedSequence.count)</div> | ||
} else { | ||
<div>@Messages("searchFoundCountWithPagination", | ||
searchItemPaginatedSequence.page * searchItemPaginatedSequence.pageSize + 1, | ||
searchItemPaginatedSequence.page * searchItemPaginatedSequence.pageSize + searchItemPaginatedSequence.items.size, | ||
searchItemPaginatedSequence.count)</div> | ||
} | ||
|
||
<table> | ||
|
||
<thead> | ||
<tr> | ||
<th>@Messages("title")</th> | ||
<th>@Messages("status")</th> | ||
<th>@Messages("currentPrice")</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
@for(item <- searchItemPaginatedSequence.items) { | ||
<tr> | ||
<td> | ||
<a href="@routes.ItemController.getItem(item.id)">@item.title</a> | ||
</td> | ||
<td>@item.itemStatus</td> | ||
<td> | ||
@if( | ||
item.itemStatus.equals(ItemStatus.Auction.toString) | ||
&& item.price.isDefined | ||
&& item.price.get > 0) { | ||
@Currency.valueOf(item.currencyId).format(item.price.get) | ||
} else { | ||
- | ||
} | ||
</td> | ||
</tr> | ||
<tr> | ||
<td colspan="4">@item.description</td> | ||
</tr> | ||
} | ||
</tbody> | ||
</table> | ||
@if(searchItemPaginatedSequence.count > searchItemPaginatedSequence.pageSize * (searchItemPaginatedSequence.page + 1)) { | ||
@foundationForm(form, routes.SearchController.search()) { | ||
@defining(form("keywords")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@field.value" name="@field.name"/> | ||
} | ||
@defining(form("maximumPrice")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@field.value" name="@field.name"/> | ||
} | ||
@defining(form("currency")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@field.value" name="@field.name"/> | ||
} | ||
@defining(form("pageNumber")) { field: Field => | ||
<input type="hidden" id="@field.id" value="@(searchItemPaginatedSequence.page + 1)" name="@field.name"/> | ||
} | ||
|
||
<input class="button" type="submit" value="@Messages("nextResultPage")"/> | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters