-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from eurofurence/feature/lost-and-found
Lost & Found First Version
- Loading branch information
Showing
16 changed files
with
345 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Eurofurence.App.Domain.Model.MongoDb/LostAndFound/LostAndFoundRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Eurofurence.App.Domain.Model.LostAndFound; | ||
using Eurofurence.App.Domain.Model.MongoDb.Repositories; | ||
using MongoDB.Driver; | ||
|
||
namespace Eurofurence.App.Domain.Model.MongoDb.LostAndFound | ||
{ | ||
public class LostAndFoundRepository : MongoDbEntityRepositoryBase<LostAndFoundRecord> | ||
{ | ||
public LostAndFoundRepository(IMongoCollection<LostAndFoundRecord> collection) | ||
: base(collection) | ||
{ | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Eurofurence.App.Domain.Model/LostAndFound/LostAndFoundRecord.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Runtime.Serialization; | ||
|
||
namespace Eurofurence.App.Domain.Model.LostAndFound | ||
{ | ||
[DataContract] | ||
public class LostAndFoundRecord : EntityBase | ||
{ | ||
public enum LostAndFoundStatusEnum | ||
{ | ||
Unknown, | ||
Lost, | ||
Found, | ||
Returned | ||
} | ||
|
||
[Required] | ||
[DataMember] | ||
public int ExternalId { get; set; } | ||
|
||
[DataMember] | ||
public string ImageUrl { get; set; } | ||
|
||
[DataMember] | ||
public string Title { get; set; } | ||
|
||
[DataMember] | ||
public string Description { get; set; } | ||
|
||
[DataMember] | ||
public LostAndFoundStatusEnum Status { get; set; } | ||
|
||
[DataMember] | ||
public DateTime? LostDateTimeUtc { get; set; } | ||
[DataMember] | ||
public DateTime? FoundDateTimeUtc { get; set; } | ||
[DataMember] | ||
public DateTime? ReturnDateTimeUtc { get; set; } | ||
|
||
|
||
|
||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Eurofurence.App.Server.Services/Abstractions/Lassie/ILassieApiClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Eurofurence.App.Server.Services.Abstractions.Lassie | ||
{ | ||
public interface ILassieApiClient | ||
{ | ||
public Task<LostAndFoundResponse[]> QueryLostAndFoundDbAsync(string command = "lostandfound"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Eurofurence.App.Server.Services/Abstractions/Lassie/LassieConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Eurofurence.App.Server.Services.Abstractions.Lassie | ||
{ | ||
public class LassieConfiguration | ||
{ | ||
public bool IsConfigured => !string.IsNullOrEmpty(ApiKey); | ||
public string BaseApiUrl { get; set; } | ||
public string ApiKey { get; set; } | ||
|
||
public static LassieConfiguration FromConfiguration(IConfiguration configuration) | ||
=> new LassieConfiguration | ||
{ | ||
BaseApiUrl = configuration["lassie:baseApiUrl"], | ||
ApiKey = configuration["lassie:apiKey"] | ||
}; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Eurofurence.App.Server.Services/Abstractions/Lassie/LostAndFoundResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace Eurofurence.App.Server.Services.Abstractions.Lassie | ||
{ | ||
public class LostAndFoundResponse | ||
{ | ||
[JsonProperty("id")] | ||
public int Id { get; set; } | ||
|
||
[JsonProperty("image")] | ||
public string ImageUrl { get; set; } | ||
|
||
[JsonProperty("title")] | ||
public string Title { get; set; } | ||
|
||
[JsonProperty("description")] | ||
public string Description { get; set; } | ||
|
||
[JsonProperty("status")] | ||
public string Status { get; set; } | ||
|
||
[JsonProperty("lost_timestamp")] | ||
public DateTime? LostDateTimeLocal { get; set; } | ||
|
||
[JsonProperty("found_timestamp")] | ||
public DateTime? FoundDateTimeLocal { get; set; } | ||
|
||
[JsonProperty("return_timestamp")] | ||
public DateTime? ReturnDateTimeLocal { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Eurofurence.App.Server.Services/Abstractions/LostAndFound/ILostAndFoundLassieImporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Eurofurence.App.Server.Services.Abstractions.LostAndFound | ||
{ | ||
public interface ILostAndFoundLassieImporter | ||
{ | ||
Task RunImportAsync(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Eurofurence.App.Server.Services/Abstractions/LostAndFound/ILostAndFoundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Eurofurence.App.Domain.Model.LostAndFound; | ||
|
||
namespace Eurofurence.App.Server.Services.Abstractions.LostAndFound | ||
{ | ||
public interface ILostAndFoundService : | ||
IEntityServiceOperations<LostAndFoundRecord>, | ||
IPatchOperationProcessor<LostAndFoundRecord> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Eurofurence.App.Server.Services/Lassie/LassieApiClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Eurofurence.App.Server.Services.Abstractions.Lassie; | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Eurofurence.App.Server.Services.Lassie | ||
{ | ||
public class LassieApiClient : ILassieApiClient | ||
{ | ||
private class DataResponseWrapper<T> | ||
{ | ||
public T[] Data { get; set; } | ||
} | ||
|
||
private LassieConfiguration _configuration; | ||
|
||
public LassieApiClient(LassieConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public async Task<LostAndFoundResponse[]> QueryLostAndFoundDbAsync(string command = "lostandfound") | ||
{ | ||
var outgoingQuery = new List<KeyValuePair<string, string>>() | ||
{ | ||
new KeyValuePair<string, string>("apikey", _configuration.ApiKey), | ||
new KeyValuePair<string, string>("request", "lostandfounddb"), | ||
new KeyValuePair<string, string>("command", command) | ||
}; | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
var response = await client.PostAsync(_configuration.BaseApiUrl, new FormUrlEncodedContent(outgoingQuery)); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
|
||
var dataResponse = JsonConvert.DeserializeObject<DataResponseWrapper<LostAndFoundResponse>>(content, | ||
new JsonSerializerSettings() { DateTimeZoneHandling = DateTimeZoneHandling.Local }); | ||
|
||
return dataResponse.Data; | ||
} | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Eurofurence.App.Server.Services/LostAndFound/LostAndFoundLassieImporter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Eurofurence.App.Common.DataDiffUtils; | ||
using Eurofurence.App.Domain.Model.LostAndFound; | ||
using Eurofurence.App.Server.Services.Abstractions.Lassie; | ||
using Eurofurence.App.Server.Services.Abstractions.LostAndFound; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Eurofurence.App.Server.Services.LostAndFound | ||
{ | ||
public class LostAndFoundLassieImporter : ILostAndFoundLassieImporter | ||
{ | ||
private ILostAndFoundService _lostAndFoundService; | ||
private ILassieApiClient _lassieApiClient; | ||
|
||
public LostAndFoundLassieImporter( | ||
ILostAndFoundService lostAndFoundService, | ||
ILassieApiClient lassieApiClient | ||
) | ||
{ | ||
_lostAndFoundService = lostAndFoundService; | ||
_lassieApiClient = lassieApiClient; | ||
} | ||
|
||
public async Task RunImportAsync() | ||
{ | ||
var existingRecords = await _lostAndFoundService.FindAllAsync(); | ||
var newRecords = await _lassieApiClient.QueryLostAndFoundDbAsync(); | ||
|
||
var patch = new PatchDefinition<LostAndFoundResponse, LostAndFoundRecord>((source, list) => | ||
list.SingleOrDefault(a => a.ExternalId == source.Id)); | ||
|
||
var statusConverter = new Func<string, LostAndFoundRecord.LostAndFoundStatusEnum>(input => | ||
{ | ||
switch(input.ToLowerInvariant()) | ||
{ | ||
case "l": return LostAndFoundRecord.LostAndFoundStatusEnum.Lost; | ||
case "f": return LostAndFoundRecord.LostAndFoundStatusEnum.Found; | ||
case "r": return LostAndFoundRecord.LostAndFoundStatusEnum.Returned; | ||
default: return LostAndFoundRecord.LostAndFoundStatusEnum.Unknown; | ||
} | ||
}); | ||
|
||
patch | ||
.Map(s => s.Id, t => t.ExternalId) | ||
.Map(s => s.Title, t => t.Title) | ||
.Map(s => s.Description, t => t.Description) | ||
.Map(s => s.ImageUrl, t => t.ImageUrl) | ||
.Map(s => statusConverter(s.Status), t => t.Status) | ||
.Map(s => s.LostDateTimeLocal?.ToUniversalTime(), t => t.LostDateTimeUtc) | ||
.Map(s => s.ReturnDateTimeLocal?.ToUniversalTime(), t => t.ReturnDateTimeUtc) | ||
.Map(s => s.FoundDateTimeLocal?.ToUniversalTime(), t => t.FoundDateTimeUtc); | ||
|
||
var patchResult = patch.Patch(newRecords, existingRecords); | ||
|
||
await _lostAndFoundService.ApplyPatchOperationAsync(patchResult); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Eurofurence.App.Server.Services/LostAndFound/LostAndFoundService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Eurofurence.App.Domain.Model.Abstractions; | ||
using Eurofurence.App.Domain.Model.LostAndFound; | ||
using Eurofurence.App.Server.Services.Abstractions; | ||
using Eurofurence.App.Server.Services.Abstractions.LostAndFound; | ||
|
||
namespace Eurofurence.App.Server.Services.LostAndFound | ||
{ | ||
public class LostAndFoundService : EntityServiceBase<LostAndFoundRecord>, ILostAndFoundService | ||
{ | ||
public LostAndFoundService( | ||
IEntityRepository<LostAndFoundRecord> entityRepository, | ||
IStorageServiceFactory storageServiceFactory | ||
) | ||
: base(entityRepository, storageServiceFactory) | ||
{ | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Eurofurence.App.Server.Web/Controllers/LostAndFoundController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Eurofurence.App.Domain.Model.LostAndFound; | ||
using Eurofurence.App.Server.Services.Abstractions.LostAndFound; | ||
using Eurofurence.App.Server.Services.Abstractions.Security; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Eurofurence.App.Server.Web.Controllers | ||
{ | ||
[Route("Api/[controller]")] | ||
public class LostAndFoundController : BaseController | ||
{ | ||
private readonly ILostAndFoundService _lostAndFoundService; | ||
private readonly IApiPrincipal _apiPrincipal; | ||
|
||
public LostAndFoundController( | ||
ILostAndFoundService lostAndFoundService, | ||
IApiPrincipal apiPrincipal | ||
) | ||
{ | ||
_lostAndFoundService = lostAndFoundService; | ||
_apiPrincipal = apiPrincipal; | ||
} | ||
|
||
[Authorize(Roles = "Attendee")] | ||
[HttpGet("Items")] | ||
public Task<IEnumerable<LostAndFoundRecord>> GetItemsAsync() | ||
{ | ||
return _lostAndFoundService.FindAllAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.