|
@@ -312,72 +312,90 @@ namespace TEAMModelOS.Controllers
|
|
|
|
|
|
[ProducesDefaultResponseType]
|
|
|
//[AuthToken(Roles = "teacher")]
|
|
|
- [HttpPost("hiteach-link")]
|
|
|
+ [HttpPost("hiteach-link-unlink")]
|
|
|
public async Task<IActionResult> HiteachLink(JsonElement request)
|
|
|
{
|
|
|
// 必要檢查
|
|
|
- if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
|
|
|
- // if (!request.TryGetProperty("linkList", out JsonElement links)) return BadRequest();
|
|
|
-
|
|
|
- if (!request.TryGetProperty("uuid", out JsonElement a)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("uuid2", out JsonElement b)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("pid", out JsonElement d)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("action", out JsonElement e)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("classId", out JsonElement c)) return BadRequest();
|
|
|
-
|
|
|
+ if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("serial_id", out JsonElement serial_id)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("opt", out JsonElement opt)) return BadRequest();
|
|
|
+ request.TryGetProperty("uuid", out JsonElement uuid);
|
|
|
+ request.TryGetProperty("uuid2", out JsonElement uuid2);
|
|
|
+ if (!request.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
|
|
|
// 如果是link classId 必填
|
|
|
- string action = e.ToString();
|
|
|
- if (action != "link" && action != "unLink") return BadRequest();
|
|
|
+ if (!$"{opt}".Equals("link") &&!$"{opt}".Equals("unlink")) return BadRequest();
|
|
|
try
|
|
|
{
|
|
|
- // [變數宣告]
|
|
|
- string school_code = code.GetString(); // 學校簡碼
|
|
|
- string uuid = a.GetString(); // uuid
|
|
|
- string uuid2 = b.GetString(); // uuid2
|
|
|
- string pid = d.GetString(); // id
|
|
|
- string classId = c.GetString(); // classId
|
|
|
-
|
|
|
-
|
|
|
// [取得DB資料]
|
|
|
var client = _azureCosmos.GetCosmosClient();
|
|
|
- var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
|
|
|
- var error = 0;
|
|
|
-
|
|
|
+ var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{serial_id}", new PartitionKey($"Product-{school_code}"));
|
|
|
+ int status = 200;
|
|
|
if (response.Status == 200)
|
|
|
{
|
|
|
- var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
-
|
|
|
- //軟體
|
|
|
- SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
|
|
|
-
|
|
|
- SerialInfoBaseWithdeviceBound updSerialInfo = schoolProductItem.serial.Where(s => s.id == pid).FirstOrDefault();
|
|
|
- if (updSerialInfo != null)
|
|
|
+ deviceBound bound = null;
|
|
|
+ var serial = JsonDocument.Parse(response.ContentStream).RootElement.Deserialize<SchoolProductSerial>();
|
|
|
+ if ($"{opt}".Equals("link"))
|
|
|
{
|
|
|
- deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.uuid == uuid && d.uuid2 == uuid2).FirstOrDefault();
|
|
|
- if (updDeviceBound != null)
|
|
|
+ if (!string.IsNullOrWhiteSpace($"{uuid}") && !string.IsNullOrWhiteSpace($"{uuid2}"))
|
|
|
{
|
|
|
- if (action.Equals("link"))
|
|
|
+ bound = serial.deviceBound.Find(x => $"{uuid}".Equals(x.uuid) && $"{uuid2}".Equals(x.uuid2));
|
|
|
+ if (bound != null)
|
|
|
{
|
|
|
- if (updDeviceBound.classId != null)
|
|
|
- {
|
|
|
- error = 1;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- updDeviceBound.classId = classId;
|
|
|
- }
|
|
|
+ bound.classId = $"{classId}";
|
|
|
}
|
|
|
- else if (action.Equals("unLink"))
|
|
|
+ }
|
|
|
+ else if (!string.IsNullOrWhiteSpace($"{uuid}") && string.IsNullOrWhiteSpace($"{uuid2}"))
|
|
|
+ {
|
|
|
+ bound = serial.deviceBound.Find(x => $"{uuid}".Equals(x.uuid));
|
|
|
+ if (bound != null)
|
|
|
{
|
|
|
- updDeviceBound.classId = null;
|
|
|
+ bound.classId = $"{classId}";
|
|
|
}
|
|
|
- if (error == 0) await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
|
|
|
}
|
|
|
-
|
|
|
+ else if (string.IsNullOrWhiteSpace($"{uuid}") && !string.IsNullOrWhiteSpace($"{uuid2}"))
|
|
|
+ {
|
|
|
+ bound = serial.deviceBound.Find(x => $"{uuid2}".Equals(x.uuid2));
|
|
|
+ if (bound != null)
|
|
|
+ {
|
|
|
+ bound.classId = $"{classId}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if ($"{opt}".Equals("unlink")) {
|
|
|
+ if (!string.IsNullOrWhiteSpace($"{uuid}") && !string.IsNullOrWhiteSpace($"{uuid2}"))
|
|
|
+ {
|
|
|
+ bound = serial.deviceBound.Find(x => $"{uuid}".Equals(x.uuid) && $"{uuid2}".Equals(x.uuid2) && $"{classId}".Equals(x.classId));
|
|
|
+ if (bound != null)
|
|
|
+ {
|
|
|
+ bound.classId = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (!string.IsNullOrWhiteSpace($"{uuid}") && string.IsNullOrWhiteSpace($"{uuid2}") )
|
|
|
+ {
|
|
|
+ bound = serial.deviceBound.Find(x => $"{uuid}".Equals(x.uuid) && $"{classId}".Equals(x.classId));
|
|
|
+ if (bound != null)
|
|
|
+ {
|
|
|
+ bound.classId = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (string.IsNullOrWhiteSpace($"{uuid}") && !string.IsNullOrWhiteSpace($"{uuid2}") )
|
|
|
+ {
|
|
|
+ bound = serial.deviceBound.Find(x => $"{uuid2}".Equals(x.uuid2) && $"{classId}".Equals(x.classId));
|
|
|
+ if (bound != null)
|
|
|
+ {
|
|
|
+ bound.classId = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (bound == null) {
|
|
|
+ status = 404;
|
|
|
}
|
|
|
+ await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolProductSerial>(serial, $"{serial_id}", new PartitionKey($"Product-{school_code}"));
|
|
|
}
|
|
|
-
|
|
|
- return Ok(new { error });
|
|
|
+ else {
|
|
|
+ status = 404;
|
|
|
+ }
|
|
|
+ return Ok(new { status });
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -389,7 +407,7 @@ namespace TEAMModelOS.Controllers
|
|
|
[ProducesDefaultResponseType]
|
|
|
//[AuthToken(Roles = "teacher")]
|
|
|
[HttpPost("hiteach-unlink-classId")]
|
|
|
- public async Task<IActionResult> HiteachUnlinkByClassId(JsonElement request)
|
|
|
+ public async Task<IActionResult> HiteachunlinkByClassId(JsonElement request)
|
|
|
{
|
|
|
// 必要檢查
|
|
|
if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
|