private List <string> Validation(ArgumentView model) { List <string> error = new List <string>(); string[] Contents = model.ConString.Split(new char[] { '|' }); string[] Sources = model.SouString.Split(new char[] { '|' }); string[] Types = model.TypeString.Split(new char[] { '|' }); if (!((Contents.Length == Sources.Length) && Contents.Length == Types.Length)) { error.Add("Błąd - niepoprawne dane przy przesylaniu "); } if (!Contents.All(x => x.Length > 0) || !Sources.All(x => x.Length > 0) || !Types.All(x => x.Length > 0)) { error.Add("Błąd - niektóre elementy są puste"); } return(error); }
public async Task <ActionResult> Add(ArgumentView model) { if (ModelState.IsValid) { List <string> erorr = Validation(model); if (erorr.Count > 0) { return(Json( new { err = JsonConvert.SerializeObject(erorr.ToArray()), Completed = false } )); } else { string[] Contents = model.ConString.Split(new char[] { '|' }); string[] Sources = model.SouString.Split(new char[] { '|' }); string[] Types = model.TypeString.Split(new char[] { '|' }); List <Argument> arguments = new List <Argument>(); for (int i = 0; i < Contents.Length; i++) { Argument arg = new Argument(); arg.Content = Contents[i]; arg.Source = Sources[i]; arg.Kind = Types[i] != "Lit" ? (Types[i] == "Fil" ? 2 : 3) : 1; arguments.Add(arg); } List <string> valErrors = ArgValidation(arguments); if (valErrors.Count > 0) { return(Json( new { err = JsonConvert.SerializeObject(valErrors.ToArray()), Completed = false } )); } else { /*List<string> werErrors = new List<string>(); * bool Completed = true; * foreach (Argument argy in arguments) * { * dynamic[] resp = { * await MyClient.ConnectWithAPI(argy.Content), * await MyClient.ConnectWithAPI(argy.Source) * }; * if(!(bool)resp[0].Passed) * { * Completed = false; * werErrors.Add("Błąd walidacji treści w argumencie " + (arguments.IndexOf(argy) + 1)); * } * if (!(bool)resp[1].Passed) * { * Completed = false; * werErrors.Add("Błąd walidacji źródła argumentu w argumencie " + (arguments.IndexOf(argy) + 1)); * } * } * dynamic[] mresp = * { * await MyClient.ConnectWithAPI(model.Question), * await MyClient.ConnectWithAPI(model.Side), * await MyClient.ConnectWithAPI(model.LibraryResource) * }; * foreach(dynamic d in mresp) * { * if(!(bool)d.Passed) * { * Completed = false; * werErrors.Add("Błąd walidacji treści w jednym z parametrów"); * } * }*/ bool Completed = true; if (Completed) { Essay newes = new Essay(); newes.Question = model.Question; newes.Side = model.Side; newes.LibraryResource = model.LibraryResource; newes.Author = TemDatabase.Autors.First(x => x.Id == model.Autor_Id); var TestDatabase = TemDatabase; TestDatabase.Essays.Add(newes); int f = await TestDatabase.SaveChangesAsync(); foreach (Argument argi in arguments) { argi.essay = newes; TestDatabase.Arguments.Add(argi); } int sf = await TestDatabase.SaveChangesAsync(); if (f >= 0 && sf >= 0) { return(Json( new { Completed = true } )); } else { return(Json( new { Completed = false, Code = f + " & " + sf, err = new string[] { "Błąd zapisu danych" } } )); } } /*else * { * return Json( * new * { * err = JsonConvert.SerializeObject(werErrors.ToArray()), * Completed = false * } * ); * }*/ } } } return(View()); }