ASP.Net核心正确实现相关模型的http方法的方法

时间:2017-03-22 11:36:40

标签: asp.net-core asp.net-core-mvc asp.net-core-webapi

我有两个模型上下文连接如下:

      <Description>
        <xsl:for-each select="images/img_item">
          <xsl:text><![CDATA[<br/><img src="]]></xsl:text>
          <xsl:value-of select="images/img_item"/>
          <xsl:text><![CDATA[" />]]></xsl:text>
        </xsl:for-each>
      </Description>

并且

public class Context
{
    [Key]
    public long ContextId { get; set; }
    [Required]
    public string Role { get; set; }

    public ICollection<Connection> Connections { get; set; }

    public Context()
    {

    }
} 

到目前为止,我没有为Connection创建任何控制器或存储库。 ContextRepositiry如下所示:

public class Connection
{
    [Key]
    public long ConnectionId { get; set; }
    [Required]
    public string Name { get; set; }

    public long ContextId { get; set; }
    public Context Context { get; set; }

    public Connection()
    {

    }
}

它是控制器:

public class ContextRepository: IContextRepository
{
    private readonly WebAPIDataContext _db;

    public ContextRepository(WebAPIDataContext db)
    {
        _db = db;
    }

    public Context CreateContext(Context context)
    {
        _db.Contexts.Add(context);
        _db.SaveChanges();
        return context;
    }

    public void DeleteContext(long id)
    {
        Context context = GetContext(id);
        if (context != null)
        {
            _db.Contexts.Remove(context);
            _db.SaveChanges();
        }
    }

    public List<Context> GetAllContexts()
    {
        return _db.Contexts.AsNoTracking().ToList();
    }

    public Context GetContext(long id)
    {
        return _db.Contexts.FirstOrDefault(o => o.ContextId == id);
    }

    public void UpdateContext(long id, Context context)
    {


    }
}

public interface IContextRepository
{
    List<Context> GetAllContexts();
    Context GetContext(long id);
    Context CreateContext(Context context);
    void UpdateContext(long id, Context context);
    void DeleteContext(long id);
}  

问题:在创建上下文时,我不必输入任何连接数据,即它应该是可选的(看起来如下招摇要求)。但是,在更新特定上下文时,可能存在连接数据,相应的上下文应相应更新。

现在,如果我输入类似的内容,请在Swagger中进行POST:

[Route("api/[controller]")]
public class ContextController : Controller
{
    private readonly IContextRepository _contexts;
    public ContextController(IContextRepository contexts)
    {
        _contexts = contexts;
    }

    [HttpGet("")]
    public IActionResult GetAllContexts()
    {
        try
        {
            List<Context> contexts = _contexts.GetAllContexts();
            return Ok(contexts);
        }
        catch (EntityNotFoundException<Context>)
        {
            return NotFound();
        }

    }

    [HttpGet("{id}")]
    public IActionResult GetContext(long id)
    {
        Context context= _contexts.GetContext(id);
        if (context == null)
        {
            return NotFound();
        }
        return Ok(context);
    }

    [HttpPost]
    public IActionResult CreateContext([FromBody] Context context)
    {
        if (ModelState.IsValid == false)
        {
            return BadRequest(ModelState);
        }

        Context createdContext= _contexts.CreateContext(context);
        if (createdContext== null)
        {
            return NotFound();
        }
        return CreatedAtAction(
             nameof(GetContext), new { id = createdContext.ContextId}, createdContext);

    }

    [HttpPut("{id}")]
    public IActionResult UpdateContext(long id, [FromBody] Context context)
    {
        if (ModelState.IsValid == false)
        {
            return BadRequest(ModelState);
        }

        try
        {
            _contexts.UpdateContext(id, context);
            return Ok();
        }
        catch (EntityNotFoundException<Context>)
        {
            return NotFound();
        }
    }

    [HttpDelete("{id}")]
    public IActionResult DeleteCOntext(long id)
    {
        _contexts.DeleteContext(id);
        return Ok();
    }
}

然后它说,{ "contextId": 0, "role": "Employee", "connections": [ { "connectionId": 0, "name": "", "contextId": 0, "context": {} } ] } The Name field is required(我试图发送像角色这样的上下文数据并留下空白连接数据 - 这应该是可能的)。如果我删除了The Role field is required,那么它会将连接设置为null,但不想从那里删除它。

0 个答案:

没有答案