@@ -83,35 +83,48 @@ class StateManagerImpl[F[_]: MonadCancelThrow](ofsManager: OfsManager[F]) extend
83
83
private def toCreateOfferName (current : State )(implicit message : Message ): F [State ] =
84
84
CreateOfferName (current).pure
85
85
86
- private def toCreateOfferPrice (current : State )(implicit message : Message ): F [State ] =
87
- message.text match {
88
- case Some (name) => CreateOfferPrice (current, Draft (name = Some (name))).pure
89
- case _ => Error (current, " Пожалуйста, введите название объявления" ).pure
90
- }
86
+ private def toCreateOfferPrice (current : State )(implicit message : Message ): F [State ] = {
87
+ val newState = for {
88
+ name <- message.text
89
+ if name.containsAtLeastOneLetterOrDigit
90
+ } yield CreateOfferPrice (current, Draft (name = Some (name)))
91
+
92
+ newState
93
+ .getOrElse(
94
+ Error (current, " Пожалуйста, введите название объявления (должно содержать хотя бы одну букву)" )
95
+ )
96
+ .pure
97
+ }
91
98
92
99
private def toCreateOfferDescription (current : State )(implicit message : Message ): F [State ] = {
93
100
val newState = for {
94
101
priceRaw <- message.text
95
102
price <- priceRaw.toIntOption
103
+ if price >= 0
96
104
draft <- current match {
97
105
case CreateOfferPrice (_, draft) => Some (draft)
98
106
case _ => None
99
107
}
100
108
updatedDraft = draft.copy(price = Some (price))
101
109
} yield CreateOfferDescription (current, updatedDraft)
102
110
103
- newState.getOrElse(Error (current, " Пожалуйста, введите цену (целое число рублей)" )).pure
111
+ newState.getOrElse(Error (current, " Пожалуйста, введите цену (целое число рублей от 0 до 2 млрд )" )).pure
104
112
}
105
113
106
114
private def toCreateOfferPhoto (current : State )(implicit message : Message ): F [State ] =
107
115
current match {
108
116
case CreateOfferDescription (_, draft) => // description has been uploaded
109
117
val newState = for {
110
118
description <- message.text
119
+ if description.containsAtLeastOneLetterOrDigit
111
120
updatedDraft = draft.copy(description = Some (description))
112
121
} yield CreateOfferPhoto (current, updatedDraft)
113
122
114
- newState.getOrElse(Error (current, " Пожалуйста, введите описание к объявлению" )).pure
123
+ newState
124
+ .getOrElse(
125
+ Error (current, " Пожалуйста, введите описание к объявлению (должно содержать хотя бы одну букву)" )
126
+ )
127
+ .pure
115
128
116
129
case CreateOfferPhoto (_, draft) => // another photo has been uploaded
117
130
val newState = for {
@@ -213,41 +226,41 @@ class StateManagerImpl[F[_]: MonadCancelThrow](ofsManager: OfsManager[F]) extend
213
226
current match {
214
227
case EditOfferName (_, offerId) =>
215
228
message.text match {
216
- case Some (newName) =>
229
+ case Some (newName) if newName.containsAtLeastOneLetterOrDigit =>
217
230
ofsManager
218
231
.updateOffer(offerId, OfferPatch (name = Some (newName)))
219
232
.handleDefaultErrors(
220
233
current,
221
234
ifSuccess = _ => UpdatedOffer (current, s " Название было изменено на \" $newName\" " ).pure
222
235
)
223
- case None =>
224
- Error (current, " Пожалуйста, введите новое название объявления" ).pure
236
+ case _ =>
237
+ Error (current, " Пожалуйста, введите новое название объявления (должно содержать хотя бы одну букву) " ).pure
225
238
}
226
239
227
240
case EditOfferPrice (_, offerId) =>
228
- message.text.flatMap(_.toIntOption) match {
241
+ message.text.flatMap(_.toIntOption).filter(_ >= 0 ) match {
229
242
case Some (newPrice) =>
230
243
ofsManager
231
244
.updateOffer(offerId, OfferPatch (price = Some (newPrice)))
232
245
.handleDefaultErrors(
233
246
current,
234
247
ifSuccess = _ => UpdatedOffer (current, s " Цена была изменена на $newPrice" ).pure
235
248
)
236
- case None =>
237
- Error (current, " Пожалуйста, введите новую цену (целое число рублей)" ).pure
249
+ case _ =>
250
+ Error (current, " Пожалуйста, введите новую цену (целое число рублей от 0 до 2 млрд )" ).pure
238
251
}
239
252
240
253
case EditOfferDescription (_, offerId) =>
241
254
message.text match {
242
- case Some (newDescription) =>
255
+ case Some (newDescription) if newDescription.containsAtLeastOneLetterOrDigit =>
243
256
ofsManager
244
257
.updateOffer(offerId, OfferPatch (description = Some (newDescription)))
245
258
.handleDefaultErrors(
246
259
current,
247
260
ifSuccess = _ => UpdatedOffer (current, s " Описание было изменено " ).pure
248
261
)
249
- case None =>
250
- Error (current, " Пожалуйста, введите новое описание объявления" ).pure
262
+ case _ =>
263
+ Error (current, " Пожалуйста, введите новое описание объявления (должно содержать хотя бы одну букву) " ).pure
251
264
}
252
265
253
266
case AddOfferPhoto (_, offerId) =>
0 commit comments