From b177bfefaf4edf38a4b028b22cd89291b9531d81 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 24 Jun 2020 19:01:08 +0000 Subject: [PATCH] --- server/AyaNova/biz/TranslationBiz.cs | 37 ++++++++++++++---------- server/AyaNova/biz/WidgetBiz.cs | 7 ++--- server/AyaNova/models/Translation.cs | 28 ++++++++++++------ server/AyaNova/models/TranslationItem.cs | 16 ++++++++-- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/server/AyaNova/biz/TranslationBiz.cs b/server/AyaNova/biz/TranslationBiz.cs index 7a1d3948..ac432ad6 100644 --- a/server/AyaNova/biz/TranslationBiz.cs +++ b/server/AyaNova/biz/TranslationBiz.cs @@ -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 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 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; diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 7fd50e7a..01d88c16 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -42,7 +42,7 @@ namespace AyaNova.Biz // internal async Task CreateAsync(Widget newObject) { - await ValidateAsync(newObject, null); + await ValidateAsync(newObject); if (HasErrors) return null; else @@ -118,7 +118,7 @@ namespace AyaNova.Biz dbObject.Tags = TagBiz.NormalizeTags(dbObject.Tags); dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields); ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency; - await ValidateAsync(dbObject, SnapshotOfOriginalDBObj); + await ValidateAsync(dbObject); if (HasErrors) return null; try { @@ -216,12 +216,11 @@ namespace AyaNova.Biz //////////////////////////////////////////////////////////////////////////////////////////////// //VALIDATION // - private async Task ValidateAsync(Widget proposedObj, Widget currentObj) + private async Task ValidateAsync(Widget proposedObj) { //NOTE: In DB schema only name and serial are not nullable //run validation and biz rules - bool isNew = currentObj == null; //Name required if (string.IsNullOrWhiteSpace(proposedObj.Name)) diff --git a/server/AyaNova/models/Translation.cs b/server/AyaNova/models/Translation.cs index a5d3cb49..4ce0f821 100644 --- a/server/AyaNova/models/Translation.cs +++ b/server/AyaNova/models/Translation.cs @@ -5,23 +5,33 @@ using System.ComponentModel.DataAnnotations; using Newtonsoft.Json; namespace AyaNova.Models -{ +{ public class Translation { + // public Translation() { } + // public Translation(Translation t) + // { + + // Id = t.Id; + // Concurrency = t.Concurrency; + // Name = t.Name; + // CjkIndex = t.CjkIndex; + // _translationItems = new List(); + // foreach (var titem in t.TranslationItems) + // { + // _translationItems.Add(new TranslationItem(titem)); + // } + // } + public long Id { get; set; } public uint Concurrency { get; set; } - + [Required] public string Name { get; set; } public bool? Stock { get; set; } public bool CjkIndex { get; set; } - //TODO: Defaults for user options translation settings: - // short date, short time formats - // currency symbol - //digit grouping separator symbol - //decimal symbol //Relationship @@ -32,12 +42,12 @@ namespace AyaNova.Models //is more efficient when there are many child collections (but when would that ever be desired for AyaNova?)and means no need to null check the collection //https://stackoverflow.com/a/20773057/8939 - private ICollection _translationItem; + private ICollection _translationItems; public virtual ICollection TranslationItems { get { - return this._translationItem ?? (this._translationItem = new HashSet()); + return this._translationItems ?? (this._translationItems = new HashSet()); } } diff --git a/server/AyaNova/models/TranslationItem.cs b/server/AyaNova/models/TranslationItem.cs index d0d48c67..c5aa9580 100644 --- a/server/AyaNova/models/TranslationItem.cs +++ b/server/AyaNova/models/TranslationItem.cs @@ -11,18 +11,28 @@ namespace AyaNova.Models public class TranslationItem { + public TranslationItem() { } + public TranslationItem(TranslationItem t) + { + Id = t.Id; + Concurrency = t.Concurrency; + Key = t.Key; + Display = t.Display; + TranslationId = t.TranslationId; + } public long Id { get; set; } public uint Concurrency { get; set; } [Required] public string Key { get; set; } - [Required] + //NOTE: due to various UI fuckery this is not set to required here so that the biz object + //validate code can fix it up automatically instead public string Display { get; set; } - + public long TranslationId { get; set; } //Relation - [JsonIgnore] + [JsonIgnore] public Translation Translation { get; set; } } }