This commit is contained in:
2020-06-24 19:01:08 +00:00
parent b4e1f4cf92
commit b177bfefaf
4 changed files with 57 additions and 31 deletions

View File

@@ -1,13 +1,10 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using AyaNova.Util;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Models;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace AyaNova.Biz
{
@@ -47,19 +44,22 @@ namespace AyaNova.Biz
//
internal async Task<Translation> PutAsync(Translation putObject)
{
Translation dbObject = await ct.Translation.SingleOrDefaultAsync(z => z.Id == putObject.Id);
Translation dbObject = await ct.Translation.Include(z => z.TranslationItems).SingleOrDefaultAsync(z => z.Id == putObject.Id);
if (dbObject == null)
{
AddError(ApiErrorCode.NOT_FOUND, "id");
return null;
}
Translation SnapshotOfOriginalDBObj = new Translation();
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
CopyObject.Copy(putObject, dbObject, "Id");
//No tags and no validation of prior state required so no snapshot required
CopyObject.Copy(putObject, dbObject, "Id");//note: won't update the child collection has to be done independently
foreach (TranslationItem ti in putObject.TranslationItems)
{
dbObject.TranslationItems.Where(z => z.Id == ti.Id).First().Display = ti.Display;
}
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
//maybe validate that there are no empty values?
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
await ValidateAsync(dbObject);
if (HasErrors) return null;
try
{
@@ -83,7 +83,7 @@ namespace AyaNova.Biz
//
internal async Task<Translation> DuplicateAsync(long id)
{
Translation dbObject = await ct.Translation.SingleOrDefaultAsync(z => z.Id == id);
Translation dbObject = await ct.Translation.Include(z => z.TranslationItems).SingleOrDefaultAsync(z => z.Id == id);
if (dbObject == null)
{
@@ -278,7 +278,7 @@ namespace AyaNova.Biz
//
//Can save or update?
private async Task ValidateAsync(Translation proposedObj, Translation currentObj)
private async Task ValidateAsync(Translation proposedObj)
{
//run validation and biz rules
@@ -291,13 +291,20 @@ namespace AyaNova.Biz
AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 char max");
//Name must be unique
if (await ct.Translation.AnyAsync(z => z.Name == proposedObj.Name))
if (await ct.Translation.AnyAsync(z => z.Name == proposedObj.Name && z.Id != proposedObj.Id))
AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name");
//Ensure there are no empty keys
if (proposedObj.TranslationItems.Where(z => z.Display.Length < 1).Any())
//Ensure there are no empty keys or too long ones
//fixing them up here rather than at the client as it's a bit of fuckery
//to try to validate or fix an item edited inside a data table with vuetify
//rather than try to deal with that just fix it here
foreach (var item in proposedObj.TranslationItems.Where(z => z.Display.Length < 1))
{
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Display", "One or more items are missing a display value");
item.Display = item.Key;
}
foreach (var item in proposedObj.TranslationItems.Where(z => z.Display.Length > 255))
{
item.Display = item.Display.Substring(0, 255);
}
return;