This commit is contained in:
@@ -32,50 +32,95 @@ namespace AyaNova.Biz
|
||||
return new TranslationBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||||
else
|
||||
return new TranslationBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//EXISTS
|
||||
internal async Task<bool> ExistsAsync(long id)
|
||||
{
|
||||
return await ct.Translation.AnyAsync(z => z.Id == id);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DUPLICATE - only way to create a new translation
|
||||
//
|
||||
internal async Task<Translation> DuplicateAsync(NameIdItem inObj)
|
||||
//UPDATE
|
||||
//
|
||||
internal async Task<Translation> PutAsync(Translation putObject)
|
||||
{
|
||||
|
||||
//make sure sourceid exists
|
||||
if (!await TranslationExistsAsync(inObj.Id))
|
||||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Id", "Source translation id does not exist");
|
||||
|
||||
//Ensure name is unique and not too long and not empty
|
||||
await ValidateAsync(inObj.Name, true);
|
||||
|
||||
if (HasErrors)
|
||||
return null;
|
||||
|
||||
//fetch the existing translation for duplication
|
||||
var SourceTranslation = await ct.Translation.Include(z => z.TranslationItems).SingleOrDefaultAsync(z => z.Id == inObj.Id);
|
||||
|
||||
//replicate the source to a new dest and save
|
||||
Translation NewTranslation = new Translation();
|
||||
NewTranslation.Name = inObj.Name;
|
||||
|
||||
NewTranslation.Stock = false;
|
||||
NewTranslation.CjkIndex = false;
|
||||
foreach (TranslationItem i in SourceTranslation.TranslationItems)
|
||||
Translation dbObject = await ct.Translation.SingleOrDefaultAsync(z => z.Id == putObject.Id);
|
||||
if (dbObject == null)
|
||||
{
|
||||
NewTranslation.TranslationItems.Add(new TranslationItem() { Key = i.Key, Display = i.Display });
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
Translation SnapshotOfOriginalDBObj = new Translation();
|
||||
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||
CopyObject.Copy(putObject, dbObject, "Id");
|
||||
|
||||
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||||
//maybe validate that there are no empty values?
|
||||
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||||
if (HasErrors) return null;
|
||||
try
|
||||
{
|
||||
await ct.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!await ExistsAsync(putObject.Id))
|
||||
AddError(ApiErrorCode.NOT_FOUND);
|
||||
else
|
||||
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||||
return null;
|
||||
}
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||||
|
||||
return dbObject;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//DUPLICATE
|
||||
//
|
||||
internal async Task<Translation> DuplicateAsync(long id)
|
||||
{
|
||||
Translation dbObject = await ct.Translation.SingleOrDefaultAsync(z => z.Id == id);
|
||||
|
||||
if (dbObject == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||||
return null;
|
||||
}
|
||||
Translation newObject = new Translation();
|
||||
//CopyObject.Copy(dbObject, newObject, "Id, Salt, Login, Password, CurrentAuthToken, DlKey, DlKeyExpire, Wiki, Serial");
|
||||
string newUniqueName = string.Empty;
|
||||
bool NotUnique = true;
|
||||
long l = 1;
|
||||
do
|
||||
{
|
||||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||||
NotUnique = await ct.Translation.AnyAsync(z => z.Name == newUniqueName);
|
||||
} while (NotUnique);
|
||||
newObject.Name = newUniqueName;
|
||||
|
||||
newObject.Stock = false;
|
||||
newObject.CjkIndex = false;
|
||||
foreach (TranslationItem i in dbObject.TranslationItems)
|
||||
{
|
||||
newObject.TranslationItems.Add(new TranslationItem() { Key = i.Key, Display = i.Display });
|
||||
}
|
||||
|
||||
//Add it to the context so the controller can save it
|
||||
await ct.Translation.AddAsync(NewTranslation);
|
||||
newObject.Id = 0;
|
||||
newObject.Concurrency = 0;
|
||||
await ct.Translation.AddAsync(newObject);
|
||||
await ct.SaveChangesAsync();
|
||||
//Log
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, NewTranslation.Id, AyaType.Translation, AyaEvent.Created), ct);
|
||||
return NewTranslation;
|
||||
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||||
// await SearchIndexAsync(newObject, true);
|
||||
// await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||||
return newObject;
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// GET
|
||||
|
||||
@@ -206,106 +251,6 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//UPDATE
|
||||
//
|
||||
|
||||
|
||||
internal async Task<bool> PutTranslationItemDisplayTextAsync(TranslationItem dbObj, NewTextIdConcurrencyTokenItem inObj, Translation dbParent)
|
||||
{
|
||||
|
||||
if (dbParent.Stock == true)
|
||||
{
|
||||
AddError(ApiErrorCode.INVALID_OPERATION, "object", "TranslationItem is from a Stock translation and cannot be modified");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Replace the db object with the PUT object
|
||||
//CopyObject.Copy(inObj, dbObj, "Id");
|
||||
dbObj.Display = inObj.NewText;
|
||||
//Set "original" value of concurrency token to input token
|
||||
//this will allow EF to check it out
|
||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
||||
|
||||
//Only thing to validate is if it has data at all in it
|
||||
if (string.IsNullOrWhiteSpace(inObj.NewText))
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Display (NewText)");
|
||||
|
||||
if (HasErrors)
|
||||
return false;
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
//Log
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbParent.Id, AyaType.Translation, AyaEvent.Modified), ct);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal async Task<bool> PutTranslationItemsDisplayTextAsync(List<NewTextIdConcurrencyTokenItem> inObj, Translation dbParent)
|
||||
{
|
||||
|
||||
if (dbParent.Stock == true)
|
||||
{
|
||||
AddError(ApiErrorCode.INVALID_OPERATION, "object", "TranslationItem is from a Stock translation and cannot be modified");
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (NewTextIdConcurrencyTokenItem tit in inObj)
|
||||
{
|
||||
var titem = await ct.TranslationItem.SingleOrDefaultAsync(z => z.Id == tit.Id);
|
||||
if (titem == null)
|
||||
{
|
||||
AddError(ApiErrorCode.NOT_FOUND, $"Translation item ID {tit.Id}");
|
||||
return false;
|
||||
}
|
||||
//Replace the db object with the PUT object
|
||||
//CopyObject.Copy(inObj, dbObj, "Id");
|
||||
titem.Display = tit.NewText;
|
||||
|
||||
//Set "original" value of concurrency token to input token
|
||||
//this will allow EF to check it out
|
||||
ct.Entry(titem).OriginalValues["Concurrency"] = tit.Concurrency;
|
||||
|
||||
//Only thing to validate is if it has data at all in it
|
||||
if (string.IsNullOrWhiteSpace(tit.NewText))
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, $"Display (NewText) for Id: {tit.Id}");
|
||||
}
|
||||
if (HasErrors)
|
||||
return false;
|
||||
await ct.SaveChangesAsync();
|
||||
|
||||
//Log
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbParent.Id, AyaType.Translation, AyaEvent.Modified), ct);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
internal async Task<bool> PutTranslationNameAsync(Translation dbObj, NewTextIdConcurrencyTokenItem inObj)
|
||||
{
|
||||
if (dbObj.Stock == true)
|
||||
{
|
||||
AddError(ApiErrorCode.INVALID_OPERATION, "object", "Translation is a Stock translation and cannot be modified");
|
||||
return false;
|
||||
}
|
||||
|
||||
dbObj.Name = inObj.NewText;
|
||||
|
||||
//Set "original" value of concurrency token to input token
|
||||
//this will allow EF to check it out
|
||||
ct.Entry(dbObj).OriginalValues["Concurrency"] = inObj.Concurrency;
|
||||
|
||||
await ValidateAsync(dbObj.Name, false);
|
||||
|
||||
if (HasErrors)
|
||||
return false;
|
||||
|
||||
await ct.SaveChangesAsync();
|
||||
//Log
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObj.Id, AyaType.Translation, AyaEvent.Modified), ct);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -333,22 +278,28 @@ namespace AyaNova.Biz
|
||||
//
|
||||
|
||||
//Can save or update?
|
||||
private async Task ValidateAsync(string inObjName, bool isNew)
|
||||
private async Task ValidateAsync(Translation proposedObj, Translation currentObj)
|
||||
{
|
||||
//run validation and biz rules
|
||||
|
||||
//Name required
|
||||
if (string.IsNullOrWhiteSpace(inObjName))
|
||||
if (string.IsNullOrWhiteSpace(proposedObj.Name))
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name");
|
||||
|
||||
//Name must be less than 255 characters
|
||||
if (inObjName.Length > 255)
|
||||
if (proposedObj.Name.Length > 255)
|
||||
AddError(ApiErrorCode.VALIDATION_LENGTH_EXCEEDED, "Name", "255 char max");
|
||||
|
||||
//Name must be unique
|
||||
if (await ct.Translation.AnyAsync(z => z.Name == inObjName))
|
||||
if (await ct.Translation.AnyAsync(z => z.Name == proposedObj.Name))
|
||||
AddError(ApiErrorCode.VALIDATION_NOT_UNIQUE, "Name");
|
||||
|
||||
//Ensure there are no empty keys
|
||||
if (proposedObj.TranslationItems.Where(z => z.Display.Length < 1).Any())
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Display", "One or more items are missing a display value");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user