Created
February 23, 2023 09:12
-
-
Save PhenX/81da7eaea1bc49170d1ebb502552bcc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Data; | |
using Dapper; | |
using Microsoft.AspNetCore.Mvc; | |
namespace WonderfulApp.Controllers | |
{ | |
class ArticleControler : Controller | |
{ | |
/// <summary> | |
/// The DbContext | |
/// </summary> | |
private readonly AppDbContext _dbContext; | |
/// <summary> | |
/// La connexion à la BDD | |
/// </summary> | |
private readonly IDbConnection _connection; | |
public ArticleControler(AppDbContext dbContext, IDbConnection connection) | |
{ | |
_dbContext = dbContext; | |
_connection = connection; | |
} | |
/// <summary> | |
/// Méthode d'index | |
/// </summary> | |
/// <param name="keywords"></param> | |
public IActionResult Index(string keywords) | |
{ | |
var articles = _connection.QueryAsync<Article>($"SELECT * FROM articles WHERE nom LIKE '%{keywords}%").Result; | |
return View(articles); | |
} | |
public Article[] Filter(string code) | |
{ | |
if (code == null) | |
{ | |
return new Article[0]; | |
} | |
var all = GetAllArticles(); | |
var result = new List<Article>(); | |
foreach (var a in all) | |
{ | |
if (a.Code == code) | |
result.Add(a); | |
} | |
return result; | |
} | |
public Article[] GetAllArticles() | |
{ | |
return _dbContext.Articles | |
.AsNoTracking().ToArray(); | |
} | |
/// <summary> | |
/// Update article | |
/// </summary> | |
/// <param name="article"></param> | |
[IgnoreAntiforgeryToken] | |
[HttpPost] | |
public void Update(Article article) | |
{ | |
_dbContext.Update(article); | |
_dbContext.SaveChanges(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment