optimized
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.IO;
|
||||
|
||||
namespace Blahblah.FlowerStory.Server.Controller;
|
||||
|
||||
@@ -293,7 +294,6 @@ public class EventApiController : BaseController
|
||||
var record = database.Records.SingleOrDefault(r => r.Id == update.Id && r.OwnerId == user.Id);
|
||||
if (record == null)
|
||||
{
|
||||
SaveDatabase();
|
||||
return NotFound(update.Id);
|
||||
}
|
||||
record.FlowerId = update.FlowerId;
|
||||
@@ -360,10 +360,11 @@ public class EventApiController : BaseController
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
var record = database.Records.SingleOrDefault(r => r.Id == id && r.OwnerId == user.Id);
|
||||
if (record == null)
|
||||
{
|
||||
SaveDatabase();
|
||||
return NotFound(id);
|
||||
}
|
||||
if (photo.Length > 0)
|
||||
@@ -371,33 +372,33 @@ public class EventApiController : BaseController
|
||||
var file = WrapFormFile(photo);
|
||||
if (file == null)
|
||||
{
|
||||
SaveDatabase();
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var p = new PhotoItem
|
||||
{
|
||||
FlowerId = record.FlowerId,
|
||||
RecordId = id,
|
||||
FileType = file.FileType,
|
||||
FileName = file.Filename,
|
||||
Path = file.Path,
|
||||
DateUploadUnixTime = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
};
|
||||
database.Photos.Add(p);
|
||||
|
||||
try
|
||||
{
|
||||
await WriteToFile(user.Id, record.FlowerId, file);
|
||||
await ExecuteTransaction(async token =>
|
||||
{
|
||||
var p = new PhotoItem
|
||||
{
|
||||
FlowerId = record.FlowerId,
|
||||
RecordId = id,
|
||||
FileType = file.FileType,
|
||||
FileName = file.Filename,
|
||||
Path = file.Path,
|
||||
DateUploadUnixTime = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
};
|
||||
AddPhotoItem(p);
|
||||
|
||||
await WriteToFile(user.Id, record.FlowerId, file, token);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SaveDatabase();
|
||||
return Problem(ex.ToString(), "api/event/add_photo");
|
||||
// TODO: Logger
|
||||
}
|
||||
}
|
||||
SaveDatabase();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
@@ -451,14 +452,12 @@ public class EventApiController : BaseController
|
||||
|
||||
if (photos == null || photos.Length == 0)
|
||||
{
|
||||
SaveDatabase();
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var record = database.Records.SingleOrDefault(r => r.Id == id && r.OwnerId == user.Id);
|
||||
if (record == null)
|
||||
{
|
||||
SaveDatabase();
|
||||
return NotFound(id);
|
||||
}
|
||||
|
||||
@@ -503,6 +502,125 @@ public class EventApiController : BaseController
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除事件关联照片
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 请求示例:
|
||||
///
|
||||
/// DELETE /api/event/remove_photo
|
||||
/// Authorization: authorization id
|
||||
///
|
||||
/// 参数:
|
||||
///
|
||||
/// id: int
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="id">图片唯一 id</param>
|
||||
/// <returns>移除成功则返回 HTTP 204</returns>
|
||||
/// <response code="204">移除成功</response>
|
||||
/// <response code="401">未找到登录会话或已过期或图片所有者不符</response>
|
||||
/// <response code="403">用户已禁用</response>
|
||||
/// <response code="404">未找到关联用户或者照片</response>
|
||||
[Route("remove_photo", Name = "removeEventPhoto")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[HttpDelete]
|
||||
public ActionResult<int> RemoveEventPhoto([FromQuery][Required] int id)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var photo = database.Photos.Where(p => p.Id == id).Include(p => p.Record).SingleOrDefault();
|
||||
if (photo == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
if (photo.Record != null && photo.Record.OwnerId != user.Id)
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
database.Photos.Remove(photo);
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
if (photo.Record != null)
|
||||
{
|
||||
DeleteFile(user.Id, photo.Record.FlowerId, photo.Path);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量移除事件关联的照片
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 请求示例:
|
||||
///
|
||||
/// POST /api/event/remove_photos
|
||||
/// Authorization: authorization id
|
||||
/// [
|
||||
/// 2, 4, 5, 11
|
||||
/// ]
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="ids">要移除的事件关联图片唯一 id 的数组</param>
|
||||
/// <returns>会话有效则返回操作影响的数据库行数</returns>
|
||||
/// <response code="200">返回操作影响的数据库行数</response>
|
||||
/// <response code="401">未找到登录会话或已过期</response>
|
||||
/// <response code="403">用户已禁用</response>
|
||||
/// <response code="404">未找到关联用户</response>
|
||||
[Route("remove_photos", Name = "removeEventPhotos")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[HttpPost]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<int> RemoveEventPhotos([FromBody] int[] ids)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (database.Photos.Any(p => ids.Contains(p.Id) && database.Records.Any(r => r.Id == p.RecordId && r.OwnerId != user.Id)))
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
var photos = database.Photos.Where(p => ids.Contains(p.Id)).Include(p => p.Record).ToList();
|
||||
var count = database.Photos.Where(p => ids.Contains(p.Id)).ExecuteDelete();
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
foreach (var photo in photos)
|
||||
{
|
||||
if (photo.Record != null)
|
||||
{
|
||||
DeleteFile(user.Id, photo.Record.FlowerId, photo.Path);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取事件关联的照片列表
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user