add some APIs
This commit is contained in:
@@ -52,6 +52,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpGet]
|
||||
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||||
public ActionResult<RecordItem[]> GetRecords(
|
||||
@@ -126,6 +127,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpDelete]
|
||||
public ActionResult<int> RemoveEvent([FromQuery][Required] int id)
|
||||
{
|
||||
@@ -170,6 +172,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpPost]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<int> RemoveEvents([FromBody] int[] ids)
|
||||
@@ -199,12 +202,16 @@ public class EventApiController : BaseController
|
||||
///
|
||||
/// POST /api/event/add
|
||||
/// Authorization: authorization id
|
||||
/// {
|
||||
/// "flowerId": 1,
|
||||
/// "eventId": 4, // 浇水
|
||||
/// "byUser": "朋友",
|
||||
/// "memo": "快干死了"
|
||||
/// }
|
||||
///
|
||||
/// 参数:
|
||||
///
|
||||
/// flowerId: 1
|
||||
/// eventId": 4 // 浇水
|
||||
/// byUser: "朋友"
|
||||
/// memo: "快干死了"
|
||||
/// lon: 29.5462794
|
||||
/// lat: 106.5380034
|
||||
/// photos: <photo>[]
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="event">事件参数</param>
|
||||
@@ -218,9 +225,12 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpPost]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<RecordItem> AddEvent([FromBody] EventParameter @event)
|
||||
[Consumes("multipart/form-data")]
|
||||
// 5 photos
|
||||
[RequestSizeLimit(75 * 1024 * 1024)]
|
||||
public async Task<ActionResult<RecordItem>> AddEvent([FromForm] EventParameter @event)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
@@ -232,19 +242,58 @@ public class EventApiController : BaseController
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var now = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var item = new RecordItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = @event.FlowerId,
|
||||
EventId = @event.EventId,
|
||||
DateUnixTime = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
EventId = @event.CategoryId,
|
||||
DateUnixTime = now,
|
||||
ByUserId = @event.ByUser == null ? user.Id : null,
|
||||
ByUserName = @event.ByUser,
|
||||
Memo = @event.Memo
|
||||
Memo = @event.Memo,
|
||||
Latitude = @event.Latitude,
|
||||
Longitude = @event.Longitude
|
||||
};
|
||||
database.Records.Add(item);
|
||||
SaveDatabase();
|
||||
|
||||
if (@event.Photos?.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ExecuteTransaction(async token =>
|
||||
{
|
||||
foreach (var photo in @event.Photos)
|
||||
{
|
||||
var file = WrapFormFile(photo);
|
||||
if (file == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var p = new PhotoItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = @event.FlowerId,
|
||||
RecordId = item.Id,
|
||||
FileType = file.FileType,
|
||||
FileName = file.Filename,
|
||||
Path = file.Path,
|
||||
DateUploadUnixTime = now
|
||||
};
|
||||
AddPhotoItem(p);
|
||||
|
||||
await WriteToFile(@event.FlowerId, file, token);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Problem(ex.ToString(), "api/event/add");
|
||||
// TODO: Logger
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(item);
|
||||
}
|
||||
|
||||
@@ -256,13 +305,17 @@ public class EventApiController : BaseController
|
||||
///
|
||||
/// PUT /api/event/update
|
||||
/// Authorization: authorization id
|
||||
/// {
|
||||
/// "id": 1,
|
||||
/// "flowerId": 1,
|
||||
/// "eventId": 5, // 施肥
|
||||
/// "byUser": null,
|
||||
/// "memo": "花多多1号"
|
||||
/// }
|
||||
///
|
||||
/// 参数:
|
||||
///
|
||||
/// id: 1
|
||||
/// flowerId: 1
|
||||
/// eventId": 5 // 施肥
|
||||
/// byUser: null
|
||||
/// memo: "花多多1号"
|
||||
/// lon: 29.5462794
|
||||
/// lat: 106.5380034
|
||||
/// photos: <photo>[]
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="update">修改参数</param>
|
||||
@@ -276,9 +329,12 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpPut]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<RecordItem> Update([FromBody] EventUpdateParameter update)
|
||||
[Consumes("multipart/form-data")]
|
||||
// 5 photos
|
||||
[RequestSizeLimit(75 * 1024 * 1024)]
|
||||
public async Task<ActionResult<RecordItem>> Update([FromForm] EventUpdateParameter update)
|
||||
{
|
||||
var (result, user) = CheckPermission();
|
||||
if (result != null)
|
||||
@@ -295,8 +351,10 @@ public class EventApiController : BaseController
|
||||
{
|
||||
return NotFound($"Event id {update.Id} not found");
|
||||
}
|
||||
var now = user.ActiveDateUnixTime ?? DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
record.FlowerId = update.FlowerId;
|
||||
record.EventId = update.EventId;
|
||||
record.EventId = update.CategoryId;
|
||||
record.DateUnixTime = now;
|
||||
if (update.ByUser == null)
|
||||
{
|
||||
record.ByUserId = user.Id;
|
||||
@@ -308,7 +366,60 @@ public class EventApiController : BaseController
|
||||
record.ByUserName = update.ByUser;
|
||||
}
|
||||
record.Memo = update.Memo;
|
||||
SaveDatabase();
|
||||
record.Latitude = update.Latitude;
|
||||
record.Longitude = update.Longitude;
|
||||
|
||||
if (update.Photos?.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ExecuteTransaction(async token =>
|
||||
{
|
||||
foreach (var photo in update.Photos)
|
||||
{
|
||||
var file = WrapFormFile(photo);
|
||||
if (file == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var photos = database.Photos.Where(p => p.RecordId == record.Id).ToList();
|
||||
if (photos.Count > 0)
|
||||
{
|
||||
database.Photos.Where(p => p.RecordId == record.Id).ExecuteDelete();
|
||||
foreach (var p in photos)
|
||||
{
|
||||
DeleteFile(update.FlowerId, p.Path);
|
||||
}
|
||||
}
|
||||
SaveDatabase();
|
||||
|
||||
var cover = new PhotoItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = update.FlowerId,
|
||||
RecordId = record.Id,
|
||||
FileType = file.FileType,
|
||||
FileName = file.Filename,
|
||||
Path = file.Path,
|
||||
DateUploadUnixTime = now
|
||||
};
|
||||
AddPhotoItem(cover);
|
||||
|
||||
await WriteToFile(update.FlowerId, file, token);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Problem(ex.ToString(), "api/event/update");
|
||||
// TODO: Logger
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveDatabase();
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
@@ -344,6 +455,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpPost]
|
||||
[Consumes("multipart/form-data")]
|
||||
[RequestSizeLimit(15 * 1024 * 1024)]
|
||||
@@ -380,6 +492,7 @@ public class EventApiController : BaseController
|
||||
{
|
||||
var p = new PhotoItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = record.FlowerId,
|
||||
RecordId = id,
|
||||
FileType = file.FileType,
|
||||
@@ -433,6 +546,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status413PayloadTooLarge)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpPost]
|
||||
[Consumes("multipart/form-data")]
|
||||
// 5 photos
|
||||
@@ -474,6 +588,7 @@ public class EventApiController : BaseController
|
||||
|
||||
var p = new PhotoItem
|
||||
{
|
||||
OwnerId = user.Id,
|
||||
FlowerId = record.FlowerId,
|
||||
RecordId = id,
|
||||
FileType = file.FileType,
|
||||
@@ -526,6 +641,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpDelete]
|
||||
public ActionResult<int> RemoveEventPhoto([FromQuery][Required] int id)
|
||||
{
|
||||
@@ -539,12 +655,13 @@ public class EventApiController : BaseController
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var photo = database.Photos.Where(p => p.Id == id).Include(p => p.Record).SingleOrDefault();
|
||||
var photo = database.Photos.Find(id);
|
||||
if (photo == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
if (photo.Record != null && photo.Record.OwnerId != user.Id)
|
||||
var item = database.Flowers.Find(photo.FlowerId);
|
||||
if (item == null || item.OwnerId != user.Id)
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
@@ -553,10 +670,7 @@ public class EventApiController : BaseController
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
if (photo.Record != null)
|
||||
{
|
||||
DeleteFile(photo.Record.FlowerId, photo.Path);
|
||||
}
|
||||
DeleteFile(photo.FlowerId ?? TempId, photo.Path);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
@@ -585,6 +699,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpPost]
|
||||
[Consumes("application/json")]
|
||||
public ActionResult<int> RemoveEventPhotos([FromBody] int[] ids)
|
||||
@@ -604,17 +719,14 @@ public class EventApiController : BaseController
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
var photos = database.Photos.Where(p => ids.Contains(p.Id)).Include(p => p.Record).ToList();
|
||||
var photos = database.Photos.Where(p => ids.Contains(p.Id)).ToList();
|
||||
var count = database.Photos.Where(p => ids.Contains(p.Id)).ExecuteDelete();
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
foreach (var photo in photos)
|
||||
{
|
||||
if (photo.Record != null)
|
||||
{
|
||||
DeleteFile(photo.Record.FlowerId, photo.Path);
|
||||
}
|
||||
DeleteFile(photo.FlowerId ?? TempId, photo.Path);
|
||||
}
|
||||
|
||||
return Ok(count);
|
||||
@@ -645,6 +757,7 @@ public class EventApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesErrorResponseType(typeof(ErrorResponse))]
|
||||
[HttpGet]
|
||||
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||||
public ActionResult<PhotoItem[]> GetPhotos([Required][FromQuery] int id)
|
||||
|
Reference in New Issue
Block a user