|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using WebApiSample.Models; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | + |
| 8 | +namespace WebApiSample.Controllers |
| 9 | +{ |
| 10 | + [Route("api/[controller]")] |
| 11 | + public class ValuesController : Controller |
| 12 | + { |
| 13 | + private SampleDbContext _dbContext { get; set; } |
| 14 | + private ILogger _logger { get; set; } |
| 15 | + |
| 16 | + public ValuesController(SampleDbContext dbContext, ILogger<ValuesController> logger) |
| 17 | + { |
| 18 | + _dbContext = dbContext; |
| 19 | + _logger = logger; |
| 20 | + } |
| 21 | + // GET api/values |
| 22 | + [HttpGet] |
| 23 | + public IActionResult Get() |
| 24 | + { |
| 25 | + var result = _dbContext.Values.OrderBy(i => i.Name); |
| 26 | + if (result != null) |
| 27 | + { |
| 28 | + return Ok(result); |
| 29 | + } |
| 30 | + return NotFound(); |
| 31 | + } |
| 32 | + |
| 33 | + // GET api/values/5 |
| 34 | + [HttpGet("{name}")] |
| 35 | + public IActionResult Get(string name) |
| 36 | + { |
| 37 | + var sql = _dbContext.Values.Where(i => i.Name == name).ToSql(); |
| 38 | + _logger.LogInformation("before sql{@}", sql); |
| 39 | + sql = sql.Replace("=", "&@~"); // AND ORは大文字 |
| 40 | + _logger.LogInformation("after sql{@}", sql); |
| 41 | + var result = _dbContext.Values.FromSql(sql).ToList(); |
| 42 | + if (result != null) |
| 43 | + { |
| 44 | + return Ok(result); |
| 45 | + } |
| 46 | + return NotFound(); |
| 47 | + } |
| 48 | + |
| 49 | + // POST api/values |
| 50 | + [HttpPost] |
| 51 | + public IActionResult Post([FromBody]Value value) |
| 52 | + { |
| 53 | + try |
| 54 | + { |
| 55 | + _dbContext.Values.Add(value); |
| 56 | + _dbContext.SaveChanges(); |
| 57 | + return Ok(); |
| 58 | + } |
| 59 | + catch (Exception ex) |
| 60 | + { |
| 61 | + _logger.LogInformation(ex.ToString()); |
| 62 | + return new NotFoundResult(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // PUT api/values/5 |
| 67 | + [HttpPut("{id}")] |
| 68 | + public IActionResult Put(int id, [FromBody]Value value) |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + var result = _dbContext.Values.Where(i => i.Id == id).First(); |
| 73 | + if(result != null) { |
| 74 | + _dbContext.Values.Update(value); |
| 75 | + _dbContext.SaveChanges(); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + _dbContext.Values.Add(value); |
| 80 | + _dbContext.SaveChanges(); |
| 81 | + } |
| 82 | + return Ok(); |
| 83 | + } |
| 84 | + catch (Exception ex) |
| 85 | + { |
| 86 | + _logger.LogInformation(ex.ToString()); |
| 87 | + return new NotFoundResult(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // DELETE api/values/5 |
| 92 | + [HttpDelete("{id}")] |
| 93 | + public IActionResult Delete(int id) |
| 94 | + { |
| 95 | + try { |
| 96 | + var value = _dbContext.Values.Where(i => i.Id == id).First(); |
| 97 | + _dbContext.Values.Remove(value); |
| 98 | + _dbContext.SaveChanges(); |
| 99 | + return Ok(); |
| 100 | + } |
| 101 | + catch (Exception ex) |
| 102 | + { |
| 103 | + _logger.LogInformation(ex.ToString()); |
| 104 | + return new NotFoundResult(); |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments