sync code
This commit is contained in:
@@ -3,6 +3,7 @@ using Blahblah.FlowerStory.Server.Data.Model;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Blahblah.FlowerStory.Server.Controller;
|
||||
|
||||
@@ -36,7 +37,9 @@ public class FlowerApiController : BaseController
|
||||
/// to: long?
|
||||
/// cfrom: decimal?
|
||||
/// cto: decimal?
|
||||
/// p: bool?
|
||||
/// photo: bool?
|
||||
/// p: int?
|
||||
/// size: int?
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="categoryId">类别 id</param>
|
||||
@@ -46,6 +49,8 @@ public class FlowerApiController : BaseController
|
||||
/// <param name="costFrom">开销最小值</param>
|
||||
/// <param name="costTo">开销最大值</param>
|
||||
/// <param name="includePhoto">是否包含封面图片</param>
|
||||
/// <param name="page">页数</param>
|
||||
/// <param name="pageSize">分页大小</param>
|
||||
/// <returns>会话有效则返回符合条件的花草集</returns>
|
||||
/// <response code="200">返回符合条件的花草集</response>
|
||||
/// <response code="401">未找到登录会话或已过期</response>
|
||||
@@ -65,7 +70,9 @@ public class FlowerApiController : BaseController
|
||||
[FromQuery(Name = "to")] long? buyTo,
|
||||
[FromQuery(Name = "cfrom")] decimal? costFrom,
|
||||
[FromQuery(Name = "cto")] decimal? costTo,
|
||||
[FromQuery(Name = "p")] bool? includePhoto)
|
||||
[FromQuery(Name = "photo")] bool? includePhoto,
|
||||
[FromQuery(Name = "p")] int? page = 0,
|
||||
[FromQuery(Name = "size")] int? pageSize = 20)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
@@ -108,6 +115,10 @@ public class FlowerApiController : BaseController
|
||||
flowers = flowers.Where(f => f.Cost != null && f.Cost <= costTo);
|
||||
}
|
||||
|
||||
var size = pageSize ?? 20;
|
||||
var p = page ?? 0;
|
||||
flowers = flowers.OrderByDescending(f => f.DateBuyUnixTime).Skip(p * size).Take(size);
|
||||
|
||||
if (includePhoto == true)
|
||||
{
|
||||
foreach (var f in flowers)
|
||||
@@ -115,12 +126,81 @@ public class FlowerApiController : BaseController
|
||||
f.Photos = database.Photos.Where(p =>
|
||||
database.Records.Any(r =>
|
||||
r.FlowerId == f.Id && r.EventId == EventCover && r.Id == p.RecordId)).ToList();
|
||||
foreach (var photo in f.Photos)
|
||||
{
|
||||
photo.Url = $"{ImageController.BaseUrl}/photo/flower/{f.Id}/{photo.Path}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(flowers.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询用户的花草
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 请求示例:
|
||||
///
|
||||
/// GET /api/flower/get
|
||||
/// Authorization: authorization id
|
||||
///
|
||||
/// 参数:
|
||||
///
|
||||
/// id: int
|
||||
/// photo: bool?
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="id">花草唯一 id</param>
|
||||
/// <param name="includePhoto">是否包含封面图片</param>
|
||||
/// <returns>会话有效则返回查询到的花草对象</returns>
|
||||
/// <response code="200">返回查询到的花草对象</response>
|
||||
/// <response code="401">未找到登录会话或已过期</response>
|
||||
/// <response code="403">用户已禁用</response>
|
||||
/// <response code="404">未找到关联用户或者未找到花草</response>
|
||||
[Route("get", Name = "getFlower")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[HttpGet]
|
||||
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||||
public ActionResult<FlowerItem> GetFlower(
|
||||
[FromQuery][Required] int id,
|
||||
[FromQuery(Name = "photo")] bool? includePhoto)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
var item = database.Flowers.Find(id);
|
||||
if (item == null)
|
||||
{
|
||||
return NotFound($"Flower id {id} not found");
|
||||
}
|
||||
|
||||
if (includePhoto == true)
|
||||
{
|
||||
item.Photos = database.Photos.Where(p =>
|
||||
database.Records.Any(r =>
|
||||
r.FlowerId == item.Id && r.EventId == EventCover && r.Id == p.RecordId)).ToList();
|
||||
foreach (var photo in item.Photos)
|
||||
{
|
||||
photo.Url = $"{ImageController.BaseUrl}/photo/flower/{item.Id}/{photo.Path}";
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除用户的花草
|
||||
/// </summary>
|
||||
@@ -221,13 +301,15 @@ public class FlowerApiController : BaseController
|
||||
///
|
||||
/// POST /api/flower/add
|
||||
/// Authorization: authorization id
|
||||
/// {
|
||||
/// "categoryId": 0,
|
||||
/// "name": "玛格丽特",
|
||||
/// "dateBuy": 1684919954743,
|
||||
/// "cost": 5.00,
|
||||
/// "purchase": "花鸟市场"
|
||||
/// }
|
||||
///
|
||||
/// 参数:
|
||||
///
|
||||
/// categoryId: 0
|
||||
/// name: "玛格丽特"
|
||||
/// dateBuy: 1684919954743
|
||||
/// cost: 5.00
|
||||
/// purchase: "花鸟市场"
|
||||
/// cover: <photo>
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="flower">花草参数</param>
|
||||
@@ -236,14 +318,17 @@ public class FlowerApiController : BaseController
|
||||
/// <response code="401">未找到登录会话或已过期</response>
|
||||
/// <response code="403">用户已禁用</response>
|
||||
/// <response code="404">未找到关联用户</response>
|
||||
/// <response code="413">提交正文过大</response>
|
||||
[Route("add", Name = "addFlower")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
|
||||
[HttpPost]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<FlowerItem> AddFlower([FromBody] FlowerParameter flower)
|
||||
[Consumes("multipart/form-data")]
|
||||
[RequestSizeLimit(5 * 1024 * 1024)]
|
||||
public async Task<ActionResult<FlowerItem>> AddFlower([FromForm] FlowerParameter flower)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
@@ -267,6 +352,57 @@ public class FlowerApiController : BaseController
|
||||
database.Flowers.Add(item);
|
||||
SaveDatabase();
|
||||
|
||||
if (flower.Cover?.Length > 0)
|
||||
{
|
||||
var file = WrapFormFile(flower.Cover);
|
||||
if (file == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var now = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var record = database.Records.SingleOrDefault(r => r.FlowerId == item.Id && r.EventId == EventCover);
|
||||
if (record == null)
|
||||
{
|
||||
record = new RecordItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = item.Id,
|
||||
EventId = EventCover,
|
||||
DateUnixTime = now,
|
||||
ByUserId = user.Id,
|
||||
ByUserName = user.Name
|
||||
//Memo = ""
|
||||
};
|
||||
database.Records.Add(record);
|
||||
}
|
||||
SaveDatabase();
|
||||
|
||||
try
|
||||
{
|
||||
await ExecuteTransaction(async token =>
|
||||
{
|
||||
var cover = new PhotoItem
|
||||
{
|
||||
FlowerId = item.Id,
|
||||
RecordId = record.Id,
|
||||
FileType = file.FileType,
|
||||
FileName = file.Filename,
|
||||
Path = file.Path,
|
||||
DateUploadUnixTime = now
|
||||
};
|
||||
AddPhotoItem(cover);
|
||||
|
||||
await WriteToFile(user.Id, item.Id, file, token);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Problem(ex.ToString(), "api/flower/add");
|
||||
// TODO: Logger
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(item);
|
||||
}
|
||||
|
||||
@@ -278,14 +414,16 @@ public class FlowerApiController : BaseController
|
||||
///
|
||||
/// PUT /api/flower/update
|
||||
/// Authorization: authorization id
|
||||
/// {
|
||||
/// "id": 0,
|
||||
/// "categoryId": 1,
|
||||
/// "name": "姬小菊",
|
||||
/// "dateBuy": 1684935276117,
|
||||
/// "cost": 15.00,
|
||||
/// "purchase": null
|
||||
/// }
|
||||
///
|
||||
/// 参数:
|
||||
///
|
||||
/// id: 0
|
||||
/// categoryId: 1
|
||||
/// name: "姬小菊"
|
||||
/// dateBuy: 1684935276117
|
||||
/// cost: 15.40
|
||||
/// purchase: null
|
||||
/// cover: <photo>
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="update">修改参数</param>
|
||||
@@ -294,14 +432,17 @@ public class FlowerApiController : BaseController
|
||||
/// <response code="401">未找到登录会话或已过期</response>
|
||||
/// <response code="403">用户已禁用</response>
|
||||
/// <response code="404">未找到关联用户或者未找到将修改的花草对象</response>
|
||||
/// <response code="413">提交正文过大</response>
|
||||
[Route("update", Name = "updateFlower")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
|
||||
[HttpPut]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<FlowerItem> Update([FromBody] FlowerUpdateParameter update)
|
||||
[Consumes("multipart/form-data")]
|
||||
[RequestSizeLimit(5 * 1024 * 1024)]
|
||||
public async Task<ActionResult<FlowerItem>> Update([FromForm] FlowerUpdateParameter update)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
@@ -316,14 +457,73 @@ public class FlowerApiController : BaseController
|
||||
var flower = database.Flowers.SingleOrDefault(f => f.Id == update.Id && f.OwnerId == user.Id);
|
||||
if (flower == null)
|
||||
{
|
||||
return NotFound(update.Id);
|
||||
return NotFound($"Flower id {update.Id} not found");
|
||||
}
|
||||
flower.CategoryId = update.CategoryId;
|
||||
flower.Name = update.Name;
|
||||
flower.DateBuyUnixTime = update.DateBuy;
|
||||
flower.Cost = update.Cost;
|
||||
flower.Purchase = update.Purchase;
|
||||
SaveDatabase();
|
||||
|
||||
if (update.Cover?.Length > 0)
|
||||
{
|
||||
var file = WrapFormFile(update.Cover);
|
||||
if (file == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var now = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var record = database.Records.SingleOrDefault(r => r.FlowerId == update.Id && r.EventId == EventCover);
|
||||
if (record == null)
|
||||
{
|
||||
record = new RecordItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = update.Id,
|
||||
EventId = EventCover,
|
||||
DateUnixTime = now,
|
||||
ByUserId = user.Id,
|
||||
ByUserName = user.Name
|
||||
//Memo = ""
|
||||
};
|
||||
database.Records.Add(record);
|
||||
}
|
||||
else
|
||||
{
|
||||
var photo = database.Photos.Where(p => p.RecordId == record.Id).SingleOrDefault();
|
||||
if (photo != null)
|
||||
{
|
||||
database.Photos.Where(p => p.RecordId == record.Id).ExecuteDelete();
|
||||
DeleteFile(user.Id, update.Id, photo.Path);
|
||||
}
|
||||
}
|
||||
SaveDatabase();
|
||||
|
||||
try
|
||||
{
|
||||
await ExecuteTransaction(async token =>
|
||||
{
|
||||
var cover = new PhotoItem
|
||||
{
|
||||
FlowerId = update.Id,
|
||||
RecordId = record.Id,
|
||||
FileType = file.FileType,
|
||||
FileName = file.Filename,
|
||||
Path = file.Path,
|
||||
DateUploadUnixTime = now
|
||||
};
|
||||
AddPhotoItem(cover);
|
||||
|
||||
await WriteToFile(user.Id, update.Id, file, token);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Problem(ex.ToString(), "api/flower/update");
|
||||
// TODO: Logger
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
@@ -377,7 +577,7 @@ public class FlowerApiController : BaseController
|
||||
var flower = database.Flowers.SingleOrDefault(f => f.Id == id && f.OwnerId == user.Id);
|
||||
if (flower == null)
|
||||
{
|
||||
return NotFound(id);
|
||||
return NotFound($"Flower id {id} not found");
|
||||
}
|
||||
if (photo.Length > 0)
|
||||
{
|
||||
|
Reference in New Issue
Block a user