iT邦幫忙

2023 iThome 鐵人賽

DAY 13
0
Cloud Native

The Journey of ASP.NET and Beyond系列 第 13

Web API Practice: Essentials For the Initial

  • 分享至 

  • xImage
  •  

Creating your character is the first step in every D&D adventure for the players. You get to choose your character’s background, skillset, appearance, personality, and add in a backstory if you’d like! D&D Beyond

  《龍與地下城》中,諸玩家之始,乃創建角色。角色由玩家扮演之,故角色之身分不得不縝密,否則玩家難以扮演。其中最基本之身分資料除姓名、種族、職業、背景外,能力值、技能、語言、特徵等等亦是身分資料之一大要點,然而若是僅以一張資料表儲存其資料,則資料庫本身將難以管理,是故將各種資料以不同形式進行管理才能有效率的管理。

製表 Create Table


  在以.NET Core Web API製作此次角色資訊之資料表前,必先知欲存之資料為何。此次練習將涉及至使用者之註冊、登入、驗證云云,故需有資料表紀錄使用者之名稱、密碼、電子信箱等,亦需有可記錄使用者資訊之資料表,以及紀錄驗證碼之資料表,以下為使用者資料表,為使UserProfile之資料表與其二者有關聯,需在製表時以public UserInfo UserInfo { get; set; }之用法作為導引,導引至UserInfo之資料表:

public partial class UserProfile
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [MaxLength(100)]
    [Required]
    [Column("UUID")]
    public string Uuid { get; set; }
    [Required]
    [MaxLength(100)]
    public string Username { get; set; }
    [MaxLength(100)]
    public string? Email { get; set; }
    [MaxLength(256)]
    public string Password { get; set; }
    public string? Avatar { get; set; }
    public DateTime Created_At { get; set; }
    public DateTime? Updated_At { get; set; }
    public UserInfo UserInfo { get; set; }
    public Verification Verification { get; set; }
    public ICollection<UserCharacters>? UserCharacters { get; set; }
}

[Table("UserInfo")]
public partial class UserInfo
{
    [Key]
    [MaxLength(100)]
    [Column("UUID")]
    public string Uuid { get; set; }
    [MaxLength(50)]
    public string Name { get; set; }

    [MaxLength(20)]
    public string Gender { get; set; }
    [MaxLength(50)]
    public string Country { get; set; }
    [MaxLength(256)]
    public string Signature { get; set; }
    public bool Verified { get; set; }
    public DateTime Updated_At { get; set; }
    public UserProfile UserProfile { get; set; }
}

[Table("Verification")]
public partial class Verification
{
    [Key]
    [MaxLength(100)]
    [Column("UUID")]
    public string Uuid { get; set; }
    [MaxLength(6)]
    public int VerificationCode { get; set; }

    public UserProfile UserProfile { get; set; }
}

  除了使用者資料外,建立「龍與地下城」之角色亦是此次練習之目的,是故必須創立角色之資料表,然而所謂角色含有許多元素,譬如角色之細節、外表、人格、故事、能力值、所持物云云,其非單一資料表所能呈現之,故這些內容可分開製表,以下為角色之資料,並非整體完成,目前先以一對一之關聯製表:

// 使用者之角色
[Table("UserCharacters")]
public partial class UserCharacters
{
    [Key]
    [Column("CharacterID")]
    public int Id { get; set; }
    [MaxLength(100)]
    public string Uuid { get; set; }
    [MaxLength(100)]
    public string Name { get; set; }
    [MaxLength(20)]
    public string? Race { get; set; }
    [MaxLength(20)]
    public string? Class { get; set; }
    [MaxLength(100)]
    public string? Avatar { get; set; }
    [DefaultValue(1)]
    public int Level { get; set; }
    public bool IsReady { get; set; }
    public DateTime Created_At { get; set; }
    public DateTime? Updated_At { get; set; }
    
    public CharacterDetails CharacterDetails { get; set; }
    public PhysicalCharacteristics PhysicalCharacteristics { get; set; }
    public PersonalCharacteristics PersonalCharacteristics { get; set; }
    public CharacterNotes CharacterNotes { get; set; }
    public AbilityScores AbilityScores { get; set; }

    public UserProfile UserProfile { get; set; }
}

// 角色之細節
[Table("CharacterDetails")]
public partial class CharacterDetails
{
    [Key]
    [Column("CharacterID")]
    [ForeignKey("UserCharacters")]
    public int Id { get; set; }
    [MaxLength(20)]
    public string? Background {  get; set; }
    public int? HitPoint { get; set; }
    [MaxLength(9)]
    public int Alignment { get; set; }
    [MaxLength(50)]
    public string? Faith { get; set; }
    [MaxLength(7)]
    public int Lifestyle { get; set; }
    [DefaultValue(30)]
    public int Speed { get; set; }

    public UserCharacters UserCharacters { get; set; }
}

// 角色之外貌
[Table("PhysicalCharacteristics")]
public partial class PhysicalCharacteristics
{
    [Key]
    [Column("CharacterID")]
    [ForeignKey("UserCharacters")]
    public int Id { get; set; }
    [MaxLength(30)]
    public string? Hair { get; set; }
    [MaxLength(30)]
    public string? Skin { get; set; }
    [MaxLength(30)]
    public string? Eyes { get; set; }
    [MaxLength(30)]
    public string? Height { get; set; }
    [MaxLength(30)]
    public string? Weight { get; set; }
    [MaxLength(30)]
    public string? Age { get; set; }
    [MaxLength(30)]
    public string? Gender { get; set; }

    public UserCharacters UserCharacters { get; set; }
}

// 角色之人格特質
[Table("PersonalCharacteristics")]
public partial class PersonalCharacteristics
{
    [Key]
    [Column("CharacterID")]
    [ForeignKey("UserCharacters")]
    public int Id { get; set; }
    [MaxLength(150)]
    public string? Personality { get; set; }
    [MaxLength(150)]
    public string? Ideals { get; set; }
    [MaxLength(150)]
    public string? Bonds { get; set; }
    [MaxLength(150)]
    public string? Flaws { get; set; }

    public UserCharacters UserCharacters { get; set; }
}

// 角色之故事
[Table("CharacterNotes")]
public partial class CharacterNotes
{
    [Key]
    [Column("CharacterID")]
    [ForeignKey("UserCharacters")]
    public int Id { get; set; }
    [MaxLength(150)]
    public string? Organizations { get; set; }
    [MaxLength(150)]
    public string? Allies { get; set; }
    [MaxLength(150)]
    public string? Enemies { get; set; }
    public string? Backstory { get; set; }
    [MaxLength(150)]
    public string? Other { get; set; }

    public UserCharacters UserCharacters { get; set; }
}

// 角色之能力值
[Table("AbilitySocres")]
public partial class AbilityScores
{
    [Key]
    [Column("CharacterID")]
    [ForeignKey("UserCharacters")]
    public int Id { get; set; }
    [MaxLength(20)]
    public int Strength { get; set; }
    [MaxLength(20)]
    public int Dexterity { get; set; }
    [MaxLength(20)]
    public int Constitution { get; set; }
    [MaxLength(20)]
    public int Intelligence { get; set; }
    [MaxLength(20)]
    public int Wisdom { get; set; }
    [MaxLength(20)]
    public int Charisma { get; set; }
    public UserCharacters UserCharacters { get; set; }
}

  註:[Key]為主鍵、[ForeignKey]為外鍵,需給予關聯之資料表。
  完成使用者及角色之資料表後便可於DbContext中進行資料表之註冊、設定:

    public partial class DnDContext: DbContext
    {
        public DnDContext(DbContextOptions<DnDContext> options) 
            : base(options)
        {
        }
        // 此處為資料表之其餘設定,可進行內容或關聯之新增
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            OnModelCreatingPartial(modelBuilder);
        }
        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

        public DbSet<UserProfile> UserProfile { get; set; }
        public DbSet<UserInfo> UserInfo { get; set; }
        public DbSet<Verification> Verifications { get; set; }
        public DbSet<UserCharacters> UserCharacters { get; set; }
        public DbSet<CharacterDetails> CharacterDetails { get; set; }
        public DbSet<PhysicalCharacteristics> PhysicalCharacteristics { get; set; }
        public DbSet<PersonalCharacteristics> PersonalCharacteristics { get; set; }
        public DbSet<CharacterNotes> CharacterNotes { get; set; }
        public DbSet<AbilityScores> AbilityScores { get; set; }
        
    }

  若是資料表之設定不完整,便可於OnModelCreating中進行修補,若修補之物過多則可添加額外檔案修補,若以額外檔案進行修補記得於DbContext中進行宣告:

public partial class DnDContext : DbContext
{
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserProfile>(entity =>
        {
            entity.HasOne(c => c.UserInfo).WithOne(e => e.UserProfile).HasForeignKey<UserInfo>(k => k.Uuid).HasPrincipalKey<UserProfile>(p => p.Uuid);
            entity.HasOne(c => c.Verification).WithOne(e => e.UserProfile).HasForeignKey<Verification>(k => k.Uuid).HasPrincipalKey<UserProfile>(p => p.Uuid);
            entity.HasMany(c => c.UserCharacters).WithOne(e => e.UserProfile).HasForeignKey(k => k.Uuid).HasPrincipalKey(p => p.Uuid);
        });
}

  製表、設定皆完成後便可於「套件管理器主控台」中輸入Add-Migration 任意文字以移轉,並輸入Update-Database進行資料庫更新。


上一篇
The Imminent Unveiling Feast of Practice
下一篇
Web API Practice: User Management
系列文
The Journey of ASP.NET and Beyond30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言