using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.Models;
using TEAMModelOS.SDK.Context.Exception;
using TEAMModelOS.SDK;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.Service.Models;
using System.Text.Json;
using TEAMModelOS.SDK.Helper.Common.StringHelper;
namespace TEAMModelOS.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CommentController :BaseController
{
private readonly AzureCosmosFactory _azureCosmos;
public CommentController(AzureCosmosFactory azureCosmos)
{
_azureCosmos = azureCosmos;
}
///
/// 添加教师评语快捷回复
///
///
///
[HttpPost("addComment")]
public async Task AddComment(CommentsDto request)
{
var id = "Comment-" + request.TEAMModelId.Replace("#", "");
ResponseBuilder builder = ResponseBuilder.custom();
List Comment = await _azureCosmos.FindByDict(new Dictionary { { "code", request.TEAMModelId }, { "id", id } });
Comment comment = new Comment();
if (Comment.IsEmpty())
{
comment.id = id;
comment.code = request.TEAMModelId;
comment.comment.Add(request.comment);
Comment.Add(comment);
}
else {
comment = Comment[0];
comment.comment.Add(request.comment);
}
builder.Data(await _azureCosmos.SaveOrUpdate(comment));
return builder.build();
}
///
/// 查询教师评语罐头
///
///
///
[HttpPost("findComment")]
public async Task findComment(JsonElement request)
{
// request.TryAdd("PartitionKey", request.lang);
ResponseBuilder builder = ResponseBuilder.custom();
List data = new List();
if (StringHelper.getKeyCount(request) > 0)
{
data = await _azureCosmos.FindByDict(request);
}
else
{
return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
}
return builder.Data(data).build();
}
///
/// 更新保存教师评语罐头,如果评语列表为空则删除
///
///
///
[HttpPost("upsertComment")]
public async Task UpsertComment(Comment request)
{
// request.TryAdd("PartitionKey", request.lang);
ResponseBuilder builder = ResponseBuilder.custom();
Comment comment = null;
if (request.comment.Count > 0)
{
if (string.IsNullOrEmpty(request.id)) {
request.id = "Comment-" + request.code.Replace("#", "");
}
comment = await _azureCosmos.SaveOrUpdate(request);
}
else {
if (!string.IsNullOrEmpty(request.id))
{
IdPk idPk = await _azureCosmos.DeleteAsync(request.id, request.code);
}
}
return builder.Data(comment).build();
}
}
}