This commit is contained in:
2021-11-24 23:02:57 +00:00
parent f2a5de5ad8
commit 492430bb8c
3 changed files with 71 additions and 1 deletions

View File

@@ -62,6 +62,33 @@ namespace AyaNova.Api.Controllers
return CreatedAtAction(nameof(UnitController.GetUnit), new { id = o.Id, version = apiVersion.ToString() }, new ApiCreatedResponse(o));
}
/// <summary>
/// Customer Create Unit
/// </summary>
/// <param name="newObject"></param>
/// <param name="apiVersion">From route path</param>
/// <returns>Id of newly created Unit or error response</returns>
[HttpPost("customerunit")]
public async Task<IActionResult> CustomerPostUnit([FromBody] CustomerPostUnit newObject, ApiVersion apiVersion)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
UnitBiz biz = UnitBiz.GetBiz(ct, HttpContext);
var customerEffectiveRights = await UserBiz.CustomerUserEffectiveRightsAsync(biz.UserId);
if (!customerEffectiveRights.CreateUnit)
{
return StatusCode(403, new ApiNotAuthorizedResponse());
}
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
long? o = await biz.CustomerCreateAsync(newObject);
if (o == null)
return BadRequest(new ApiErrorResponse(biz.Errors));
else
return Ok(ApiOkResponse.Response(o));
}
/// <summary>
/// Get Unit
/// </summary>
@@ -348,7 +375,7 @@ namespace AyaNova.Api.Controllers
}
}
return Ok(ApiOkResponse.Response(new{contract=retContract,IsMetered=UnitInfo.Metered}));
return Ok(ApiOkResponse.Response(new { contract = retContract, IsMetered = UnitInfo.Metered }));
}

View File

@@ -60,6 +60,35 @@ namespace AyaNova.Biz
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//CREATE (Customer version)
//
internal async Task<long?> CustomerCreateAsync(CustomerPostUnit postObject)
{
Unit newObject=new Unit();
newObject.Serial=postObject.Serial;
newObject.Description=postObject.Description;
newObject.CustomerId=postObject.CustomerId;
newObject.UnitModelId=postObject.UnitModelId;
await ValidateAsync(newObject, null);
if (HasErrors)
return null;
else
{
newObject.Tags = TagBiz.NormalizeTags(newObject.Tags);
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
await ct.Unit.AddAsync(newObject);
await ct.SaveChangesAsync();
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
await SearchIndexAsync(newObject, true);
await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
await HandlePotentialNotificationEvent(AyaEvent.Created, newObject);
return newObject.Id;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
//GET
//

View File

@@ -112,6 +112,20 @@ namespace AyaNova.Models
}//eoc
//DTO object used when Customer posts a new unit of their own when using CSR form
public class CustomerPostUnit
{
[Required]
public string Serial { get; set; }
[Required]
public long CustomerId { get; set; }
public long? UnitModelId { get; set; }
public string Description { get; set; }
}//eoc
}//eons
/*