forked from welserb/20486D
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
PiAlexay
committed
Jun 8, 2022
1 parent
9914ab3
commit ca63eab
Showing
6 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Allfiles/Mod06/Labfiles/01_ButterfliesShop_begin/ButterfliesShop/Models/IndexViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace ButterfliesShop.Models | ||
{ | ||
public class IndexViewModel | ||
{ | ||
public List<Butterfly> Butterflies { get; set; } | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...les/01_ButterfliesShop_begin/ButterfliesShop/Validators/MaxButterflyQuantityValidation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Threading.Tasks; | ||
using ButterfliesShop.Models; | ||
using ButterfliesShop.Services; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ButterfliesShop.Validators | ||
{ | ||
public class MaxButterflyQuantityValidation : ValidationAttribute | ||
{ | ||
private int _maxAmount; | ||
public MaxButterflyQuantityValidation(int maxAmount) | ||
{ | ||
_maxAmount = maxAmount; | ||
} | ||
|
||
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | ||
{ | ||
|
||
var service = (IButterfliesQuantityService)validationContext.GetService(typeof(IButterfliesQuantityService)); | ||
Butterfly butterfly = (Butterfly)validationContext.ObjectInstance; | ||
if (butterfly.ButterflyFamily != null) | ||
{ | ||
int? quantity = service.GetButterflyFamilyQuantity(butterfly.ButterflyFamily.Value); | ||
int? sumQuantity = quantity + butterfly.Quantity; | ||
if (sumQuantity > _maxAmount) | ||
{ | ||
return new ValidationResult(string.Format("Limit of butterflies from the same family in the store is {0} butterflies. Currently there are {1}", _maxAmount, quantity)); | ||
} | ||
return ValidationResult.Success; | ||
} | ||
return ValidationResult.Success; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters