Bearbeitung von Fragen

This commit is contained in:
2020-06-10 13:23:29 +02:00
parent 6d84a4614b
commit a8b04a6443
11 changed files with 205 additions and 34 deletions

View File

@@ -0,0 +1,43 @@
using MultipleChoiceTrainer.Models.DataModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace MultipleChoiceTrainer.Validation
{
public class NotEmptyChoiceCollectionAttribute : ValidationAttribute
{
public NotEmptyChoiceCollectionAttribute()
{
}
public override bool IsValid(object value)
{
ErrorMessage = "Es müssen mindestens zwei Antwortmöglichkeiten eingegeben werden!";
if (value == null || !(value is ICollection<Choice> collection))
{
return false;
}
if(collection == null || collection.Where(e => !string.IsNullOrEmpty(e.Text)).Count() < 2)
{
return false;
}
if(!collection.Where(e => !string.IsNullOrEmpty(e.Text)).Any(e => e.IsTrue))
{
ErrorMessage = "Es muss mindestens eine richtige Antwort eingegeben werden!";
return false;
}
return true;
}
}
}