|
| 1 | +/* |
| 2 | + * Copyright 2026 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.amend |
| 18 | + |
| 19 | +import controllers.actions._ |
| 20 | +import forms.amend.WhatDoYouWantToAmendStandardFormProvider |
| 21 | +import javax.inject.Inject |
| 22 | +import models.NormalMode |
| 23 | +import navigation.Navigator |
| 24 | +import pages.amend.WhatDoYouWantToAmendStandardPage |
| 25 | +import play.api.i18n.{I18nSupport, MessagesApi} |
| 26 | +import play.api.mvc.{Action, AnyContent, MessagesControllerComponents} |
| 27 | +import repositories.SessionRepository |
| 28 | +import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendBaseController |
| 29 | +import views.html.amend.WhatDoYouWantToAmendStandardView |
| 30 | + |
| 31 | +import scala.concurrent.{ExecutionContext, Future} |
| 32 | + |
| 33 | +class WhatDoYouWantToAmendStandardController @Inject() ( |
| 34 | + override val messagesApi: MessagesApi, |
| 35 | + sessionRepository: SessionRepository, |
| 36 | + navigator: Navigator, |
| 37 | + identify: IdentifierAction, |
| 38 | + getData: DataRetrievalAction, |
| 39 | + requireData: DataRequiredAction, |
| 40 | + formProvider: WhatDoYouWantToAmendStandardFormProvider, |
| 41 | + val controllerComponents: MessagesControllerComponents, |
| 42 | + view: WhatDoYouWantToAmendStandardView |
| 43 | +)(implicit ec: ExecutionContext) |
| 44 | + extends FrontendBaseController |
| 45 | + with I18nSupport { |
| 46 | + |
| 47 | + val form = formProvider() |
| 48 | + |
| 49 | + def onPageLoad(): Action[AnyContent] = (identify andThen getData andThen requireData) { implicit request => |
| 50 | + |
| 51 | + val preparedForm = request.userAnswers.get(WhatDoYouWantToAmendStandardPage) match { |
| 52 | + case None => form |
| 53 | + case Some(value) => form.fill(value) |
| 54 | + } |
| 55 | + |
| 56 | + Ok(view(preparedForm)) |
| 57 | + } |
| 58 | + |
| 59 | + def onSubmit(): Action[AnyContent] = (identify andThen getData andThen requireData).async { implicit request => |
| 60 | + form |
| 61 | + .bindFromRequest() |
| 62 | + .fold( |
| 63 | + formWithErrors => Future.successful(BadRequest(view(formWithErrors))), |
| 64 | + value => |
| 65 | + for { |
| 66 | + updatedAnswers <- Future.fromTry(request.userAnswers.set(WhatDoYouWantToAmendStandardPage, value)) |
| 67 | + _ <- sessionRepository.set(updatedAnswers) |
| 68 | + } yield Redirect(navigator.nextPage(WhatDoYouWantToAmendStandardPage, NormalMode, updatedAnswers)) |
| 69 | + ) |
| 70 | + } |
| 71 | +} |
0 commit comments