iT邦幫忙

2023 iThome 鐵人賽

DAY 8
0
Cloud Native

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

ASP.NET Core Web API: CRUD Adventurer's Guild

  • 分享至 

  • xImage
  •  

In the heart of the realm, where magic and sword meet, lies the Adventurer's Guild, a venerable institution steeped in the lore of ages. 

  於換態旅店經過一晚,是日風和日麗,街道上熙熙攘攘,復行數步,可見冒險者三五成群,乃前往「增刪查改」。「增刪查改」冒險者公會,坐落於市中心,其屋儼然,「CRUD」此四字刻在木質招牌上清晰可見。公會內人聲鼎沸,吆喝酌酒者絡繹不絕,鬥毆鬧事者叫囂不斷……。此處乃冒險者之根據地,受事、組隊、註冊等種種事項,皆於其處理。

禮賓部 RESTful API Style


  禮賓部,乃謂公會之門面,其最為人所知乃「增刪查改」之行事風格,如同換態旅店之侍者,為諸冒險者之交流途徑,顧名思義,其工作以成冒險者之請求,以助冒險者處理種種公會之事項。

增 Create


  成為冒險者之前必先於公會登記辦理,而其流程便是其一,禮賓部給予公會制式之登記表。其二,欲成冒險者登記該表再繳回。其三,禮賓部回應辦理狀態。以下可見範例:
  制式登記表DTO:

    public class InformationDto
    {
        public string Name { get; set; }
        public string Class { get; set; }
        public string Race { get; set; }
        public string Dwelling { get; set; }
    }

  於禮賓部註冊冒險者身分:

[HttpPost]
public async Task<IActionResult> Post(InformationDto Information)
{
    AdventurerProfile profile = new AdventurerProfile()
    {
        Sigil = Guid.NewGuid().ToString(),
        Name = Information.Name,
        Race = Information.Race,
        Class = Information.Class,
        Dwelling = Information.Dwelling,
        Created_At = DateTime.Now
    };

    _Context.AdventurersProfile.Add(profile);
    try
    {
        await _Context.SaveChangesAsync();
    }
    catch (Exception ex)
    {
        return Ok("1");
    }
    return Ok(0);
}

刪 Delete


  欲註銷冒險者身分者亦於禮賓部登記:

[HttpDelete]
public async Task<IActionResult> Delete([FromForm]string Sigil)
{
    var adventurer = _Context.AdventurersProfile.FirstOrDefault(e =>  e.Sigil == Sigil);
    _Context.AdventurersProfile.Remove(adventurer);
    try
    {
        await _Context.SaveChangesAsync();
    }
    catch (Exception ex)
    {
        return Ok("1");
    }
    return Ok(0);
}

查 Read


  欲查冒險者登記之資料者於禮賓部詢問:

[HttpGet]
public async Task<IActionResult> Get()
{
    var adventurer = _Context.AdventurersProfile;
    return adventurer == null ? NotFound() : Ok(adventurer);
}

改 Update


  欲更改冒險者登記之資料者,可於禮賓部登記:

[HttpPut("Sigil")]
public async Task<IActionResult> Put(string Sigil, InformationDto information)
{
    var adventurer = _Context.AdventurersProfile.FirstOrDefault(e => e.Sigil == Sigil);
    if (adventurer != null)
    {
        adventurer.Name = information.Name;
        adventurer.Class = information.Class;
        adventurer.Race = information.Race;
        adventurer.Dwelling = information.Dwelling;
        adventurer.Updated_At = DateTime.Now;
        _Context.AdventurersProfile.Update(adventurer);
        try
        {
            await _Context.SaveChangesAsync();
        }
        catch (Exception ex)
        {
            return Ok("1");
        }
        return Ok(0);
    }
    else
    {
        return BadRequest();
    }

}

  完成應可見「Post」、「Get」、「Delete」、「Update」等字樣。
https://ithelp.ithome.com.tw/upload/images/20231004/2015124483wGW1IBVB.png


上一篇
ASP.NET Core Web API: RESTful API Inn
下一篇
ASP.NET Core Web API: Identity Recognition
系列文
The Journey of ASP.NET and Beyond30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言