1
0
mirror of synced 2025-01-18 22:24:06 +01:00

Implement music read, fix artist nullable

This commit is contained in:
asesidaa 2023-02-13 02:40:40 +08:00
parent 1db9fdf6d3
commit 1fccca42c6
5 changed files with 66 additions and 4 deletions

View File

@ -0,0 +1,33 @@
using System.Xml.Serialization;
namespace Application.Dto;
public class MusicDto
{
[XmlAttribute(AttributeName = "id")]
public int Id { get; set; }
[XmlElement("music_id")]
public int MusicId { get; set; }
[XmlElement(ElementName = "title")]
public string Title { get; set; } = string.Empty;
[XmlElement(ElementName = "artist")]
public string Artist { get; set; } = string.Empty;
[XmlElement(ElementName = "release_date")]
public string ReleaseDate { get; set; } = "2013-01-01 08:00:00";
[XmlElement(ElementName = "end_date")]
public string EndDate { get; set; } = "2030-01-01 08:00:00";
[XmlElement("new_flag")]
public int NewFlag { get; set; }
[XmlElement("use_flag")]
public int UseFlag { get; set; }
[XmlElement("calc_flag")]
public int CalcFlag { get; set; }
}

View File

@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using Application.Common.Extensions;
using Application.Common.Models;
using Application.Interfaces;
using Application.Mappers;
using Domain.Enums;
using Microsoft.EntityFrameworkCore;
@ -11,12 +13,26 @@ public record ReadMusicQuery(long CardId) : IRequestWrapper<string>;
public class ReadMusicQueryHandler : CardRequestHandlerBase<ReadMusicQuery, string>
{
private const string MUSIC_XPATH = "/root/music/record";
public ReadMusicQueryHandler(ICardDependencyAggregate aggregate) : base(aggregate)
{
}
public override Task<ServiceResult<string>> Handle(ReadMusicQuery request, CancellationToken cancellationToken)
[SuppressMessage("ReSharper.DPA", "DPA0007: Large number of DB records",
Justification = "To return all musics, the whole table need to be returned")]
public override async Task<ServiceResult<string>> Handle(ReadMusicQuery request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
var musics = await MusicDbContext.MusicUnlocks.ToListAsync(cancellationToken: cancellationToken);
var dtoList = musics.Select((unlock, i) =>
{
var dto = unlock.MusicToDto();
dto.Id = i;
dto.CalcFlag = Random.Shared.NextDouble() >= 0.5 ? 1 : 0;
return dto;
});
var result = dtoList.SerializeCardDataList(MUSIC_XPATH);
return new ServiceResult<string>(result);
}
}

View File

@ -0,0 +1,13 @@
using Application.Dto;
using Domain.Entities;
using Riok.Mapperly.Abstractions;
namespace Application.Mappers;
[Mapper]
public static partial class MusicMapper
{
public static partial MusicDto MusicToDto(this MusicUnlock music);
private static int BoolToInt(bool value) => value ? 1 : 0;
}

View File

@ -6,7 +6,7 @@ public partial class MusicUnlock
public string Title { get; set; } = string.Empty;
public string Artist { get; set; } = string.Empty;
public string? Artist { get; set; } = string.Empty;
public DateTime ReleaseDate { get; set; }

View File

@ -67,7 +67,7 @@ public partial class MusicDbContext : DbContext, IMusicDbContext
entity.Property(e => e.MusicId)
.ValueGeneratedNever()
.HasColumnName("music_id");
entity.Property(e => e.Artist).HasColumnName("artist");
entity.Property(e => e.Artist).HasColumnName("artist").IsRequired(false);
entity.Property(e => e.Title).HasColumnName("title");
entity.Property(e => e.ReleaseDate).HasColumnName("release_date");
entity.Property(e => e.EndDate).HasColumnName("end_date");