如何在cs文件中创建一个函数“HeroesUpdate”

问题描述 投票:0回答:1

在“HeroController.cs”API控制器中,您需要执行以下操作[23分]

  • 创建“HeroesUpdate”公共 Put 端点/函数,以允许从 GetHeroesByFilterAsync 存储库函数。此外,用户应该能够通过以下方式更新记录: 用新的字符串值替换现有的字符串值。注意:端点/函数路由名称应该 与端点/函数名称相同。此外,端点/函数是异步的 端点/方法采用两个“字符串”作为参数(现有的和新的字符串值)并返回 “IActionResult”(8 分)。
  • 因此,您需要通过“GetHeroesByFilterAsync”函数获取现有的英雄数据: 将现有搜索字符串值作为参数传递给 try/catch 块(3 分)。
  • 如果从包含现有搜索的“GetHeroesByFilterAsync”函数调用返回记录 字符串值,您必须替换 Hero 模型中任何字符串属性中的现有字符串值。 换句话说,将现有字符串值替换为新字符串值,无论它在任何 varchar 列。例如,如果“名称”列中的文本为“Bruce Banner (The Hulk)”并且 您现有的字符串值为“Bruce”,新字符串值为“Peanut”,然后名称中的新文本 更新后的栏目应为“花生横幅(绿巨人)”。此功能应该发生在 表的列和跨记录。换句话说,它应该更新多个列并且 同时匹配现有值的记录(9 分)。
  • 成功更新后,端点/函数必须返回 200(Ok)响应,其中包含文本“The 英雄已成功更新”(1分)。
  • 如果不存在包含该现有搜索字符串值的记录,则端点/函数必须返回 NotFound 响应,返回文本“文本存在的英雄不存在”。和 “existingValue”是文本,您传递到端点(1 分)。
  • 如果发生任何异常,应该在 catch 块中捕获它并返回 500(InternalServerError) 响应消息“发生意外问题。请创建一张支持票。”应该 返回(1 分)。
[Httpput]
[Route("UpdateHero/(HeroID)")]
public async Task<ActionResult>HeroUpdate(string filter, HeroViewModel herovimodel)
{
    try
    {
        var excistingHero 
    }
}

[Httpput]
[Route("UpdateHero/(HeroID)")]
public async Task<ActionResult>HeroUpdate(string filter, HeroViewModel herovimodel)
{
    try
    {
        var excistingHero 
    }
}
c#
1个回答
-1
投票
// Begining of controller
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

[ApiController]
[Route("[controller]")]
public class HeroesController : ControllerBase
{
    private readonly IRepository _repository;

    public HeroesController(IRepository repository)
    {
        _repository = repository;
    }

    [HttpPut]
    [Route("HeroesUpdate")]
    public async Task<IActionResult> HeroesUpdate(string existing, string newString)
    {
        try
        {
            var heroes = await _repository.GetHeroesByFilterAsync(existing);

            if (heroes.Any())
            {
                foreach (var hero in heroes)
                {
                    hero.Name = hero.Name.Replace(existing, newString);
                    hero.Description = hero.Description.Replace(existing, newString);
                    await _repository.UpdateAsync(hero);
                }

                return Ok("The heroes have been updated successfully");
            }
            else
            {
                return NotFound($"Heroes with the text {existing} do not exist");
            }
        }
        catch (Exception ex)
        {
            // Logging the exception
            return StatusCode(500, "An unexpected problem occurred. Please create a support ticket.");
        }
    }
}

//END OF CONTROLLER

with this assumes we have repository and the model setup. :

//HeroRepository.cs://(Repositories/HeroRepository.s)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class HeroRepository : IRepository
{
    private readonly List<Hero> _heroes = new List<Hero>
    {
        new Hero { Id = 1, Name = "Superman", Description = "The Man of Steel" },
        new Hero { Id = 2, Name = "Batman", Description = "The Dark Knight" },
    };

    public async Task<List<Hero>> GetHeroesByFilterAsync(string filter)
    {
        return _heroes.Where(h => h.Name.Contains(filter)).ToList();
    }

    public async Task UpdateAsync(Hero hero)
    {
        var existingHero = _heroes.FirstOrDefault(h => h.Id == hero.Id);
        if (existingHero != null)
        {
            existingHero.Name = hero.Name;
            existingHero.Description = hero.Description;
            // Update other properties 
        }
    }
    // Implement other repository methods if available/required
}  IRepository.cs://(Repositories/IRepositoy.cs)
using System.Collections.Generic;
using System.Threading.Tasks;

public interface IRepository
{
    Task<List<Hero>> GetHeroesByFilterAsync(string filter);
    Task UpdateAsync(Hero hero);
    //  other repository methods 
}
//(Models/HeroViewModel.cs)
Hero.cs:// Models/Hero.cs
public class Hero
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    
}
HeroViewModel.cs: //(Models/HeroViewModel.cs)
public class HeroViewModel
{
    public string Name { get; set; }
    public string Description { get; set; }
    // Add other properties as needed
}  ```
© www.soinside.com 2019 - 2024. All rights reserved.