|
| 1 | +/* |
| 2 | + * Copyright 2025 HM Revenue & Customs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package controllers.purchaser |
| 18 | + |
| 19 | +import controllers.actions.* |
| 20 | +import forms.purchaser.PurchaserCTUTRFormProvider |
| 21 | +import models.purchaser.WhoIsMakingThePurchase |
| 22 | +import models.purchaser.WhoIsMakingThePurchase.Company |
| 23 | +import models.{Mode, NormalMode} |
| 24 | +import navigation.Navigator |
| 25 | +import pages.purchaser.{NameOfPurchaserPage, PurchaserCTUTRPage, WhoIsMakingThePurchasePage} |
| 26 | +import play.api.i18n.{I18nSupport, MessagesApi} |
| 27 | +import play.api.mvc.{Action, AnyContent, MessagesControllerComponents} |
| 28 | +import repositories.SessionRepository |
| 29 | +import services.purchaser.PurchaserService |
| 30 | +import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController |
| 31 | +import views.html.purchaser.PurchaserCTUTRView |
| 32 | + |
| 33 | +import javax.inject.Inject |
| 34 | +import scala.concurrent.{ExecutionContext, Future} |
| 35 | + |
| 36 | +class PurchaserCTUTRController @Inject()( |
| 37 | + override val messagesApi: MessagesApi, |
| 38 | + sessionRepository: SessionRepository, |
| 39 | + navigator: Navigator, |
| 40 | + identify: IdentifierAction, |
| 41 | + getData: DataRetrievalAction, |
| 42 | + requireData: DataRequiredAction, |
| 43 | + purchaserService: PurchaserService, |
| 44 | + formProvider: PurchaserCTUTRFormProvider, |
| 45 | + val controllerComponents: MessagesControllerComponents, |
| 46 | + view: PurchaserCTUTRView |
| 47 | + )(implicit ec: ExecutionContext) extends FrontendBaseController with I18nSupport { |
| 48 | + |
| 49 | + val form = formProvider() |
| 50 | + |
| 51 | + def onPageLoad(mode: Mode): Action[AnyContent] = (identify andThen getData andThen requireData) { |
| 52 | + implicit request => |
| 53 | + |
| 54 | + val userAnswers = request.userAnswers |
| 55 | + |
| 56 | + userAnswers.get(NameOfPurchaserPage) match { |
| 57 | + case None => Redirect(controllers.purchaser.routes.NameOfPurchaserController.onPageLoad(NormalMode)) |
| 58 | + |
| 59 | + case Some(value) => |
| 60 | + val purchaserName: String = value.fullName |
| 61 | + |
| 62 | + val preparedForm = request.userAnswers.get(PurchaserCTUTRPage) match { |
| 63 | + case None => form |
| 64 | + case Some(value) => form.fill(value) |
| 65 | + } |
| 66 | + |
| 67 | + val continueRoute = Ok(view(preparedForm, purchaserName, mode)) |
| 68 | + purchaserService.checkPurchaserType(Company, userAnswers, continueRoute) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + def onSubmit(mode: Mode): Action[AnyContent] = (identify andThen getData andThen requireData).async { |
| 73 | + implicit request => |
| 74 | + |
| 75 | + request.userAnswers.get(NameOfPurchaserPage) match { |
| 76 | + case None => Future.successful(Redirect(controllers.purchaser.routes.NameOfPurchaserController.onPageLoad(NormalMode))) |
| 77 | + |
| 78 | + case Some(value) => |
| 79 | + val purchaserName: String = value.fullName |
| 80 | + |
| 81 | + form.bindFromRequest().fold( |
| 82 | + formWithErrors => |
| 83 | + Future.successful(BadRequest(view(formWithErrors, purchaserName, mode))), |
| 84 | + |
| 85 | + value => |
| 86 | + for { |
| 87 | + updatedAnswers <- Future.fromTry(request.userAnswers.set(PurchaserCTUTRPage, value)) |
| 88 | + _ <- sessionRepository.set(updatedAnswers) |
| 89 | + } yield Redirect(navigator.nextPage(PurchaserCTUTRPage, mode, updatedAnswers)) |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments