add latitude & longitude
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Blahblah.FlowerStory.Server.Data.Model;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@@ -38,6 +39,9 @@ public class FlowerApiController : BaseController
|
||||
/// cfrom: decimal?
|
||||
/// cto: decimal?
|
||||
/// photo: bool?
|
||||
/// lon: double?
|
||||
/// lat: double?
|
||||
/// distance: int?
|
||||
/// p: int?
|
||||
/// size: int?
|
||||
///
|
||||
@@ -49,6 +53,9 @@ public class FlowerApiController : BaseController
|
||||
/// <param name="costFrom">开销最小值</param>
|
||||
/// <param name="costTo">开销最大值</param>
|
||||
/// <param name="includePhoto">是否包含封面图片</param>
|
||||
/// <param name="latitude">纬度</param>
|
||||
/// <param name="longitude">经度</param>
|
||||
/// <param name="distance">距离(米)</param>
|
||||
/// <param name="page">页数</param>
|
||||
/// <param name="pageSize">分页大小</param>
|
||||
/// <returns>会话有效则返回符合条件的花草集</returns>
|
||||
@@ -63,7 +70,7 @@ public class FlowerApiController : BaseController
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[HttpGet]
|
||||
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
|
||||
public ActionResult<FlowerItem[]> GetFlowers(
|
||||
public ActionResult<FlowerResult> GetFlowers(
|
||||
[FromQuery(Name = "cid")] int? categoryId,
|
||||
[FromQuery] string? key,
|
||||
[FromQuery(Name = "from")] long? buyFrom,
|
||||
@@ -71,6 +78,9 @@ public class FlowerApiController : BaseController
|
||||
[FromQuery(Name = "cfrom")] decimal? costFrom,
|
||||
[FromQuery(Name = "cto")] decimal? costTo,
|
||||
[FromQuery(Name = "photo")] bool? includePhoto,
|
||||
[FromQuery(Name = "lat")] double? latitude,
|
||||
[FromQuery(Name = "lon")] double? longitude,
|
||||
[FromQuery] int? distance,
|
||||
[FromQuery(Name = "p")] int? page = 0,
|
||||
[FromQuery(Name = "size")] int? pageSize = 20)
|
||||
{
|
||||
@@ -86,7 +96,7 @@ public class FlowerApiController : BaseController
|
||||
|
||||
SaveDatabase();
|
||||
|
||||
var flowers = database.Flowers.Where(f => f.OwnerId == user.Id);
|
||||
IEnumerable<FlowerItem> flowers = database.Flowers.Where(f => f.OwnerId == user.Id);
|
||||
if (categoryId != null)
|
||||
{
|
||||
flowers = flowers.Where(f => f.CategoryId == categoryId);
|
||||
@@ -115,6 +125,20 @@ public class FlowerApiController : BaseController
|
||||
flowers = flowers.Where(f => f.Cost != null && f.Cost <= costTo);
|
||||
}
|
||||
|
||||
if (distance != null && latitude != null && longitude != null)
|
||||
{
|
||||
flowers = flowers.Where(f => f.Latitude != null && f.Longitude != null)
|
||||
.AsEnumerable()
|
||||
.Where(f =>
|
||||
{
|
||||
var d = GetDistance(latitude.Value, longitude.Value, f.Latitude ?? 0, f.Longitude ?? 0);
|
||||
f.Distance = (int)d;
|
||||
return d <= distance;
|
||||
});
|
||||
}
|
||||
|
||||
int count = flowers.Count();
|
||||
|
||||
var size = pageSize ?? 20;
|
||||
var p = page ?? 0;
|
||||
flowers = flowers.OrderByDescending(f => f.DateBuyUnixTime).Skip(p * size).Take(size);
|
||||
@@ -128,12 +152,16 @@ public class FlowerApiController : BaseController
|
||||
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}";
|
||||
photo.Url = $"photo/flower/{f.Id}/{photo.Path}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(flowers.ToArray());
|
||||
return Ok(new FlowerResult
|
||||
{
|
||||
Flowers = flowers.ToArray(),
|
||||
Count = count
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -194,7 +222,7 @@ public class FlowerApiController : BaseController
|
||||
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}";
|
||||
photo.Url = $"photo/flower/{item.Id}/{photo.Path}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,7 +421,7 @@ public class FlowerApiController : BaseController
|
||||
};
|
||||
AddPhotoItem(cover);
|
||||
|
||||
await WriteToFile(user.Id, item.Id, file, token);
|
||||
await WriteToFile(item.Id, file, token);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -491,11 +519,14 @@ public class FlowerApiController : BaseController
|
||||
}
|
||||
else
|
||||
{
|
||||
var photo = database.Photos.Where(p => p.RecordId == record.Id).SingleOrDefault();
|
||||
if (photo != null)
|
||||
var photos = database.Photos.Where(p => p.RecordId == record.Id).ToList();
|
||||
if (photos.Count > 0)
|
||||
{
|
||||
database.Photos.Where(p => p.RecordId == record.Id).ExecuteDelete();
|
||||
DeleteFile(user.Id, update.Id, photo.Path);
|
||||
foreach (var photo in photos)
|
||||
{
|
||||
DeleteFile(update.Id, photo.Path);
|
||||
}
|
||||
}
|
||||
}
|
||||
SaveDatabase();
|
||||
@@ -515,7 +546,7 @@ public class FlowerApiController : BaseController
|
||||
};
|
||||
AddPhotoItem(cover);
|
||||
|
||||
await WriteToFile(user.Id, update.Id, file, token);
|
||||
await WriteToFile(update.Id, file, token);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -524,8 +555,12 @@ public class FlowerApiController : BaseController
|
||||
// TODO: Logger
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SaveDatabase();
|
||||
}
|
||||
|
||||
return Ok(user);
|
||||
return Ok(flower);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -620,7 +655,7 @@ public class FlowerApiController : BaseController
|
||||
};
|
||||
AddPhotoItem(cover);
|
||||
|
||||
await WriteToFile(user.Id, id, file, token);
|
||||
await WriteToFile(id, file, token);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
Reference in New Issue
Block a user