This commit is contained in:
2020-02-24 23:07:33 +00:00
parent f5871e125f
commit 1140856278
2 changed files with 59 additions and 0 deletions

View File

@@ -168,7 +168,40 @@ namespace AyaNova.Api.Controllers
}
/// <summary>
/// Duplicate DataListView
/// </summary>
/// <param name="id">Create a duplicate of this items id</param>
/// <param name="apiVersion">Automatically filled from route path, no need to specify in body</param>
/// <returns></returns>
[HttpPost("duplicate/{id}")]
public async Task<IActionResult> Duplicate([FromRoute] long id, ApiVersion apiVersion)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
//Instantiate the business object handler
DataListViewBiz biz = DataListViewBiz.GetBiz(ct, HttpContext);
//If a user has change roles
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
return StatusCode(403, new ApiNotAuthorizedResponse());
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
var oSrc = await biz.GetAsync(id, false);
if (oSrc == null)
return NotFound(new ApiErrorResponse(ApiErrorCode.NOT_FOUND));
//Create and validate
DataListView o = await biz.DuplicateAsync(oSrc);
if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors));
else
return CreatedAtAction(nameof(DataListViewController.GetDataListView), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
}
/// <summary>
/// Delete DataListView

View File

@@ -76,6 +76,32 @@ namespace AyaNova.Biz
////////////////////////////////////////////////////////////////////////////////////////////////
//DUPLICATE
//
internal async Task<DataListView> DuplicateAsync(DataListView dbObj)
{
DataListView outObj = new DataListView();
CopyObject.Copy(dbObj, outObj);
outObj.Name = Util.StringUtil.NameUniquify(outObj.Name, 255);
outObj.Id = 0;
outObj.ConcurrencyToken = 0;
await ct.DataListView.AddAsync(outObj);
await ct.SaveChangesAsync();
//Handle child and associated items:
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, outObj.Id, BizType, AyaEvent.Created), ct);
return outObj;
}
////////////////////////////////////////////////////////////////////////////////////////////////
/// GET