-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement endpoint to add presents - Sort dropdowns
- Loading branch information
1 parent
82d97eb
commit 8f25035
Showing
14 changed files
with
269 additions
and
39 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
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
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
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
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
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
19 changes: 19 additions & 0 deletions
19
DragaliaAPI/DragaliaAPI/Features/Web/Savefile/SavefileEditRequest.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,19 @@ | ||
using System.Text.Json.Serialization; | ||
using DragaliaAPI.Shared.Definitions.Enums; | ||
|
||
namespace DragaliaAPI.Features.Web.Savefile; | ||
|
||
public class SavefileEditRequest | ||
{ | ||
public required List<PresentFormSubmission> Presents { get; init; } | ||
} | ||
|
||
public class PresentFormSubmission | ||
{ | ||
[JsonConverter(typeof(JsonStringEnumConverter<EntityTypes>))] | ||
public EntityTypes Type { get; init; } | ||
|
||
public int Item { get; init; } | ||
|
||
public int Quantity { get; init; } | ||
} |
117 changes: 117 additions & 0 deletions
117
DragaliaAPI/DragaliaAPI/Features/Web/Savefile/SavefileEditService.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,117 @@ | ||
using DragaliaAPI.Database; | ||
using DragaliaAPI.Features.Present; | ||
using DragaliaAPI.Shared.Definitions.Enums; | ||
using DragaliaAPI.Shared.Features.Presents; | ||
using DragaliaAPI.Shared.MasterAsset; | ||
|
||
namespace DragaliaAPI.Features.Web.Savefile; | ||
|
||
internal sealed partial class SavefileEditService( | ||
IPresentService presentService, | ||
ApiContext apiContext, | ||
ILogger<SavefileEditService> logger | ||
) | ||
{ | ||
public bool ValidateEdits(SavefileEditRequest request) => ValidatePresents(request.Presents); | ||
|
||
public async Task PerformEdits(SavefileEditRequest request) | ||
{ | ||
foreach (PresentFormSubmission presentFormSubmission in request.Presents) | ||
{ | ||
presentService.AddPresent( | ||
new Present.Present( | ||
PresentMessage.DragaliaLostTeamGift, // Could consider adding a new message to the master asset | ||
presentFormSubmission.Type, | ||
presentFormSubmission.Item, | ||
presentFormSubmission.Quantity | ||
) | ||
); | ||
} | ||
|
||
Log.AddedPresents(logger, request.Presents.Count); | ||
|
||
await apiContext.SaveChangesAsync(); | ||
|
||
Log.EditSuccessful(logger); | ||
} | ||
|
||
private bool ValidatePresents(List<PresentFormSubmission> presents) | ||
{ | ||
if (presents.Count > 100) | ||
{ | ||
Log.PresentLimitExceeded(logger, presents.Count); | ||
return false; | ||
} | ||
|
||
foreach (PresentFormSubmission present in presents) | ||
{ | ||
if (present.Quantity < 1) | ||
{ | ||
Log.InvalidSinglePresent(logger, present); | ||
return false; | ||
} | ||
|
||
bool idValid = present.Type switch | ||
{ | ||
EntityTypes.Chara => ValidateCharaPresent(present), | ||
EntityTypes.Dragon => ValidateDragonPresent(present), | ||
EntityTypes.Item => ValidateItemPresent(present), | ||
EntityTypes.Material => ValidateMaterialPresent(present), | ||
EntityTypes.DmodePoint => ValidateDmodePointPresent(present), | ||
EntityTypes.SkipTicket => ValidateSkipTicketPresent(present), | ||
EntityTypes.DragonGift => ValidateDragonGiftPresent(present), | ||
_ => false, | ||
}; | ||
|
||
if (!idValid) | ||
{ | ||
Log.InvalidSinglePresent(logger, present); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static bool ValidateCharaPresent(PresentFormSubmission present) => | ||
MasterAsset.CharaData.ContainsKey((Charas)present.Item) && present.Quantity == 1; | ||
|
||
private static bool ValidateDragonPresent(PresentFormSubmission present) => | ||
MasterAsset.DragonData.ContainsKey((Dragons)present.Item); | ||
|
||
private static bool ValidateItemPresent(PresentFormSubmission present) => | ||
MasterAsset.UseItem.ContainsKey((UseItem)present.Item); | ||
|
||
private static bool ValidateMaterialPresent(PresentFormSubmission present) => | ||
MasterAsset.MaterialData.ContainsKey((Materials)present.Item); | ||
|
||
private static bool ValidateDmodePointPresent(PresentFormSubmission present) => | ||
(DmodePoint)present.Item is DmodePoint.Point1 or DmodePoint.Point2; | ||
|
||
private static bool ValidateSkipTicketPresent(PresentFormSubmission present) => | ||
present.Item == 0; | ||
|
||
private static bool ValidateDragonGiftPresent(PresentFormSubmission present) => | ||
Enum.IsDefined((DragonGifts)present.Item); | ||
|
||
private static partial class Log | ||
{ | ||
[LoggerMessage( | ||
LogLevel.Information, | ||
"Request was invalid: {Count} presents exceeds limit of 100" | ||
)] | ||
public static partial void PresentLimitExceeded(ILogger logger, int count); | ||
|
||
[LoggerMessage(LogLevel.Information, "Request was invalid: present {@Present} was invalid")] | ||
public static partial void InvalidSinglePresent( | ||
ILogger logger, | ||
PresentFormSubmission present | ||
); | ||
|
||
[LoggerMessage(LogLevel.Information, "Added {Count} presents to the gift box")] | ||
public static partial void AddedPresents(ILogger logger, int count); | ||
|
||
[LoggerMessage(LogLevel.Information, "Savefile edited successfully")] | ||
public static partial void EditSuccessful(ILogger logger); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
PUBLIC_ENABLE_MSW=true # Enables Mock Service Worker for API calls and decreases cookie security. | ||
PUBLIC_VERSION=development | ||
PUBLIC_ENABLE_SAVE_EDITOR=true | ||
|
||
DAWNSHARD_API_URL_SSR=http://localhost:5000/ |
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.