Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar
(authenticate andThen getData).async { implicit request =>
val preparedForm = request.userAnswers.getOrElse(UserAnswers(request.credId)).get(HowManyParkingSpacesOrGaragesIncludedInRentPage) match {
case None => form
case Some(value) => form.fill(HowManyParkingSpacesOrGaragesIncludedInRentForm(value.uncoveredSpaces.toInt, value.coveredSpaces.toInt, value.garages.toInt))
case Some(value) => form.fill(HowManyParkingSpacesOrGaragesIncludedInRentForm(
value.uncoveredSpaces,
value.coveredSpaces,
value.garages
))
}
Future.successful(Ok(howManyParkingSpacesOrGaragesIncludedInRentView(
form = preparedForm,
Expand All @@ -81,7 +85,6 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar
formWithErrors => {
val formWithCorrectedErrors = formWithErrors.errors.head match {
case value if value.key.isEmpty &&
value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.allFields.error.required") ||
value.messages.contains("howManyParkingSpacesOrGaragesIncludedInRent.error.required") =>
val uncoveredSpaces = value.copy(key = "uncoveredSpaces")
val coveredSpaces = value.copy(key = "coveredSpaces")
Expand All @@ -99,7 +102,7 @@ class HowManyParkingSpacesOrGaragesIncludedInRentController @Inject()(howManyPar
)))
},
parkingSpacesOrGaragesIncluded =>
val answers = HowManyParkingSpacesOrGarages(parkingSpacesOrGaragesIncluded.uncoveredSpaces.toString, parkingSpacesOrGaragesIncluded.coveredSpaces.toString, parkingSpacesOrGaragesIncluded.garages.toString)
val answers = HowManyParkingSpacesOrGarages(parkingSpacesOrGaragesIncluded.uncoveredSpaces, parkingSpacesOrGaragesIncluded.coveredSpaces, parkingSpacesOrGaragesIncluded.garages)
for {
updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId)).set(HowManyParkingSpacesOrGaragesIncludedInRentPage, answers))
_ <- sessionRepository.set(updatedAnswers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2025 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.ngrraldfrontend.controllers

import play.api.i18n.I18nSupport
import play.api.mvc.{Action, AnyContent, MessagesControllerComponents}
import uk.gov.hmrc.ngrraldfrontend.actions.{AuthRetrievals, DataRetrievalAction}
import uk.gov.hmrc.ngrraldfrontend.config.AppConfig
import uk.gov.hmrc.ngrraldfrontend.models.components.*
import uk.gov.hmrc.ngrraldfrontend.models.components.NGRRadio.buildRadios
import uk.gov.hmrc.ngrraldfrontend.models.forms.RepairsAndInsuranceForm
import uk.gov.hmrc.ngrraldfrontend.models.forms.RepairsAndInsuranceForm.form
import uk.gov.hmrc.ngrraldfrontend.models.{Mode, RepairsAndInsurance, UserAnswers}
import uk.gov.hmrc.ngrraldfrontend.navigation.Navigator
import uk.gov.hmrc.ngrraldfrontend.pages.RepairsAndInsurancePage
import uk.gov.hmrc.ngrraldfrontend.repo.SessionRepository
import uk.gov.hmrc.ngrraldfrontend.views.html.RepairsAndInsuranceView
import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class RepairsAndInsuranceController @Inject()(repairsAndInsuranceView: RepairsAndInsuranceView,
authenticate: AuthRetrievals,
navigator: Navigator,
getData: DataRetrievalAction,
sessionRepository: SessionRepository,
mcc: MessagesControllerComponents)(implicit appConfig: AppConfig, ec: ExecutionContext)
extends FrontendController(mcc) with I18nSupport {

def show(mode: Mode): Action[AnyContent] = {
(authenticate andThen getData).async { implicit request =>
val preparedForm = request.userAnswers.getOrElse(UserAnswers(request.credId)).get(RepairsAndInsurancePage) match {
case None => form
case Some(value) => form.fill(RepairsAndInsuranceForm(value.internalRepairs, value.externalRepairs, value.buildingInsurance))
}
Future.successful(Ok(repairsAndInsuranceView(
form = preparedForm,
internalRepairs = buildRadios(preparedForm, RepairsAndInsuranceForm.ngrRadio(preparedForm, "internalRepairs")),
externalRepairs = buildRadios(preparedForm, RepairsAndInsuranceForm.ngrRadio(preparedForm, "externalRepairs")),
buildingInsurance = buildRadios(preparedForm, RepairsAndInsuranceForm.ngrRadio(preparedForm, "buildingInsurance")),
propertyAddress = request.property.addressFull,
mode
)))
}
}

def submit(mode: Mode): Action[AnyContent] =
(authenticate andThen getData).async { implicit request =>
form.bindFromRequest().fold(
formWithErrors => {
Future.successful(BadRequest(repairsAndInsuranceView(
form = formWithErrors,
internalRepairs = buildRadios(formWithErrors, RepairsAndInsuranceForm.ngrRadio(formWithErrors, "internalRepairs")),
externalRepairs = buildRadios(formWithErrors, RepairsAndInsuranceForm.ngrRadio(formWithErrors, "externalRepairs")),
buildingInsurance = buildRadios(formWithErrors, RepairsAndInsuranceForm.ngrRadio(formWithErrors, "buildingInsurance")),
propertyAddress = request.property.addressFull,
mode
)))
},
repairsAndInsurance =>
val answers = RepairsAndInsurance(repairsAndInsurance.internalRepairs, repairsAndInsurance.externalRepairs, repairsAndInsurance.buildingInsurance)
for{
updatedAnswers <- Future.fromTry(request.userAnswers.getOrElse(UserAnswers(request.credId)).set(RepairsAndInsurancePage, answers))
_ <- sessionRepository.set(updatedAnswers)
} yield Redirect(navigator.nextPage(RepairsAndInsurancePage, mode, updatedAnswers))
)
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package uk.gov.hmrc.ngrraldfrontend.models
import play.api.libs.json.{Json, OFormat}

case class HowManyParkingSpacesOrGarages(
uncoveredSpaces: String,
coveredSpaces: String,
garages: String,
uncoveredSpaces: Int,
coveredSpaces: Int,
garages: Int,
)

object HowManyParkingSpacesOrGarages {
Expand Down
51 changes: 0 additions & 51 deletions app/uk/gov/hmrc/ngrraldfrontend/models/RaldUserAnswers.scala

This file was deleted.

29 changes: 29 additions & 0 deletions app/uk/gov/hmrc/ngrraldfrontend/models/RepairsAndInsurance.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.ngrraldfrontend.models

import play.api.libs.json.{Json, OFormat}

case class RepairsAndInsurance(
internalRepairs: String,
externalRepairs: String,
buildingInsurance: String
)

object RepairsAndInsurance {
implicit val format: OFormat[RepairsAndInsurance] = Json.format[RepairsAndInsurance]
}
10 changes: 10 additions & 0 deletions app/uk/gov/hmrc/ngrraldfrontend/models/components/NGRRadio.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ case object rentIncWaterChargesNo extends WhatYourRentIncludesRadio
case object rentIncServiceYes extends WhatYourRentIncludesRadio
case object rentIncServiceNo extends WhatYourRentIncludesRadio

sealed trait RepairsAndInsurance extends RadioEntry
case object InternalRepairsYou extends RepairsAndInsurance
case object InternalRepairsLandlord extends RepairsAndInsurance
case object InternalRepairsYouAndLandlord extends RepairsAndInsurance
case object ExternalRepairsYou extends RepairsAndInsurance
case object ExternalRepairsLandlord extends RepairsAndInsurance
case object ExternalRepairsYouAndLandlord extends RepairsAndInsurance
case object BuildingInsuranceYou extends RepairsAndInsurance
case object BuildingInsuranceLandlord extends RepairsAndInsurance
case object LandlordYouAndLandlord extends RepairsAndInsurance

case class NGRRadioName(key: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat
Constraint((input: A) => {
val formData = input.asInstanceOf[HowManyParkingSpacesOrGaragesIncludedInRentForm]
(formData.uncoveredSpaces, formData.coveredSpaces, formData.garages) match {
case (-1,-1,-1) => Invalid(allFieldsRequiredError)
case (0, 0, 0) => Invalid(fieldRequired)
case (uncoveredSpaces, coveredSpaces, garages) if uncoveredSpaces + coveredSpaces + garages <= 0 => Invalid(fieldRequired)
case (_,_,_)=> Valid
}
})
Expand All @@ -73,7 +72,7 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat
isLargerThanInt(maxValue, uncoveredSpacesTooHighError)
)
)
).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString)),
).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString)),
"coveredSpaces" -> optional(
text()
.transform[String](_.strip().replaceAll(",", ""), identity)
Expand All @@ -83,7 +82,7 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat
isLargerThanInt(maxValue, coveredSpacesTooHighError)
)
)
).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString)),
).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString)),
"garages" -> optional(
text()
.transform[String](_.strip().replaceAll(",", ""), identity)
Expand All @@ -93,7 +92,7 @@ object HowManyParkingSpacesOrGaragesIncludedInRentForm extends CommonFormValidat
isLargerThanInt(maxValue, garagesTooHighError)
)
)
).transform[Int](_.map(_.toInt).getOrElse(-1), value => Some(value.toString)),
).transform[Int](_.map(_.toInt).getOrElse(0), value => Some(value.toString)),
)
(HowManyParkingSpacesOrGaragesIncludedInRentForm.apply)(HowManyParkingSpacesOrGaragesIncludedInRentForm.unapply)
.verifying(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2025 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.ngrraldfrontend.models.forms

import play.api.data.Form
import play.api.data.Forms.mapping
import play.api.i18n.*
import play.api.libs.json.{Json, OFormat}
import uk.gov.hmrc.govukfrontend.views.Aliases.Text
import uk.gov.hmrc.govukfrontend.views.viewmodels.fieldset.Legend
import uk.gov.hmrc.ngrraldfrontend.models.components.*
import uk.gov.hmrc.ngrraldfrontend.models.forms.DoesYourRentIncludeParkingForm.radioText
import uk.gov.hmrc.ngrraldfrontend.models.forms.mappings.Mappings

final case class RepairsAndInsuranceForm(
internalRepairs: String,
externalRepairs: String,
buildingInsurance: String
)

object RepairsAndInsuranceForm extends CommonFormValidators with Mappings {
implicit val format: OFormat[RepairsAndInsuranceForm] = Json.format[RepairsAndInsuranceForm]

private lazy val internalRepairsRequiredError = "repairsAndInsurance.internalRepairs.radio.required.error"
private lazy val externalRepairsRequiredError = "repairsAndInsurance.externalRepairs.radio.required.error"
private lazy val buildingInsuranceRequiredError = "repairsAndInsurance.buildingInsurance.radio.required.error"
private val internalRepairsRadio = "repairsAndInsurance-internalRepairs-radio-value"
private val externalRepairsRadio = "repairsAndInsurance-externalRepairs-radio-value"
private val buildingInsuranceRadio = "repairsAndInsurance-buildingInsurance-radio-value"

val messagesApi: MessagesApi = new DefaultMessagesApi()
val lang: Lang = Lang.defaultLang
val messages: Messages = MessagesImpl(lang, messagesApi)

def youButton(radioType: RadioEntry): NGRRadioButtons = NGRRadioButtons(radioContent = "repairsAndInsurance.radio.you", radioValue = radioType)
def landlordButton(radioType: RadioEntry): NGRRadioButtons = NGRRadioButtons(radioContent = "repairsAndInsurance.radio.landlord", radioValue = radioType)
def youAndLandlordButton(radioType: RadioEntry): NGRRadioButtons = NGRRadioButtons(radioContent = "repairsAndInsurance.radio.youAndLandlord", radioValue = radioType)

def unapply(repairsAndInsuranceForm: RepairsAndInsuranceForm): Option[(String, String, String)] =
Some(repairsAndInsuranceForm.internalRepairs, repairsAndInsuranceForm.externalRepairs, repairsAndInsuranceForm.buildingInsurance)

def ngrRadio(form: Form[RepairsAndInsuranceForm], radioType:String)(implicit messages: Messages): NGRRadio = {
val buttons: Seq[NGRRadioButtons] = radioType match {
case value if value.contains("internalRepairs") => Seq(youButton(InternalRepairsYou), landlordButton(InternalRepairsLandlord), youAndLandlordButton(InternalRepairsYouAndLandlord))
case value if value.contains("externalRepairs") => Seq(youButton(ExternalRepairsYou), landlordButton(ExternalRepairsLandlord), youAndLandlordButton(ExternalRepairsYouAndLandlord))
case _ => Seq(youButton(BuildingInsuranceYou), landlordButton(BuildingInsuranceLandlord), youAndLandlordButton(LandlordYouAndLandlord))
}
NGRRadio(
NGRRadioName(s"repairsAndInsurance-$radioType-radio-value"),
ngrTitle = Some(Legend(content = Text(messages(s"repairsAndInsurance.$radioType.radio.label")),
classes = "govuk-fieldset__legend--m", isPageHeading = true)),
NGRRadioButtons = buttons
)
}

def form: Form[RepairsAndInsuranceForm] = {
Form(
mapping(
internalRepairsRadio -> radioText(internalRepairsRequiredError),
externalRepairsRadio -> radioText(externalRepairsRequiredError),
buildingInsuranceRadio -> radioText(buildingInsuranceRequiredError),
)(RepairsAndInsuranceForm.apply)(RepairsAndInsuranceForm.unapply)
)
}
}
4 changes: 2 additions & 2 deletions app/uk/gov/hmrc/ngrraldfrontend/navigation/Navigator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ class Navigator @Inject()() {
case None => uk.gov.hmrc.ngrraldfrontend.controllers.routes.CheckRentFreePeriodController.show(NormalMode)
}
case RentFreePeriodPage => _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.RentDatesAgreeStartController.show(NormalMode)
//TODO need to connect to next pages when they are done
case DoYouPayExtraForParkingSpacesPage => answers =>
answers.get(DoYouPayExtraForParkingSpacesPage) match {
case Some(value) => value match {
case true => uk.gov.hmrc.ngrraldfrontend.controllers.routes.DoYouPayExtraForParkingSpacesController.show(NormalMode)
case _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.DoYouPayExtraForParkingSpacesController.show(NormalMode)
case _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.RepairsAndInsuranceController.show(NormalMode)
}
case None => throw new NotFoundException("Failed to find answers")
}
case RepairsAndInsurancePage => _ => uk.gov.hmrc.ngrraldfrontend.controllers.routes.RentDatesAgreeStartController.show(NormalMode) //TODO Page not made yet
}

//TODO change to check your answers page
Expand Down
Loading