flamingo.me/flamingo-commerce/v3@v3.11.0/category/interfaces/controller/controller.go (about) 1 package controller 2 3 import ( 4 "context" 5 6 "flamingo.me/flamingo/v3/framework/web" 7 8 "flamingo.me/flamingo-commerce/v3/category/application" 9 "flamingo.me/flamingo-commerce/v3/category/domain" 10 productApplication "flamingo.me/flamingo-commerce/v3/product/application" 11 searchDomain "flamingo.me/flamingo-commerce/v3/search/domain" 12 "flamingo.me/flamingo-commerce/v3/search/utils" 13 ) 14 15 type ( 16 // ViewController provides web-specific actions for category single view 17 ViewController struct { 18 commandHandler QueryHandler 19 breadcrumbService *application.BreadcrumbService 20 responder *web.Responder 21 router *web.Router 22 template string 23 teaserTemplate string 24 } 25 26 // ViewData for rendering context 27 ViewData struct { 28 ProductSearchResult *productApplication.SearchResult 29 Category domain.Category 30 CategoryTree domain.Tree 31 SearchMeta searchDomain.SearchMeta 32 PaginationInfo utils.PaginationInfo 33 } 34 ) 35 36 // Inject the ViewController controller required dependencies 37 func (vc *ViewController) Inject( 38 queryCommandHandler QueryHandler, 39 breadcrumbService *application.BreadcrumbService, 40 responder *web.Responder, 41 router *web.Router, 42 config *struct { 43 Template string `inject:"config:commerce.category.view.template"` 44 TeaserTemplate string `inject:"config:commerce.category.view.teaserTemplate"` 45 }, 46 ) *ViewController { 47 vc.commandHandler = queryCommandHandler 48 vc.responder = responder 49 vc.router = router 50 vc.breadcrumbService = breadcrumbService 51 52 if config != nil { 53 vc.template = config.Template 54 vc.teaserTemplate = config.TeaserTemplate 55 } 56 57 return vc 58 } 59 60 // Get Action to display a category page for the web 61 func (vc *ViewController) Get(c context.Context, request *web.Request) web.Result { 62 63 result, redirect, err := vc.commandHandler.Execute(c, Request{ 64 Code: request.Params["code"], 65 Name: request.Params["name"], 66 URL: *request.Request().URL, 67 QueryAll: request.QueryAll(), 68 }) 69 70 if err == domain.ErrNotFound || err == searchDomain.ErrNotFound { 71 return vc.responder.NotFound(err) 72 } 73 if err != nil { 74 return vc.responder.ServerError(err) 75 } 76 77 if redirect != nil { 78 redirectParams := map[string]string{ 79 "code": redirect.Code, 80 "name": redirect.Name, 81 } 82 u, _ := vc.router.Relative("category.view", redirectParams) 83 u.RawQuery = request.QueryAll().Encode() 84 return vc.responder.URLRedirect(u).Permanent() 85 } 86 87 // Deprecated 88 vc.breadcrumbService.AddBreadcrumb(c, result.CategoryTree) 89 90 var template string 91 switch result.Category.CategoryType() { 92 case domain.TypeTeaser: 93 template = vc.teaserTemplate 94 default: 95 template = vc.template 96 } 97 98 return vc.responder.Render(template, ViewData{ 99 ProductSearchResult: result.ProductSearchResult, 100 Category: result.Category, 101 CategoryTree: result.CategoryTree, 102 SearchMeta: result.SearchMeta, 103 PaginationInfo: result.PaginationInfo, 104 }) 105 }