|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using NETCore.Repository; |
| 4 | +using NETCore.Repository.Data; |
| 5 | +using NETCore.Repository.Interface; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Net; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace NETCore.Base |
| 13 | +{ |
| 14 | + [Route("api/[controller]")] |
| 15 | + [ApiController] |
| 16 | + public class BaseController<Entity, Repository, Key> : ControllerBase |
| 17 | + where Entity : class |
| 18 | + where Repository : IRepository<Entity, Key> |
| 19 | + |
| 20 | + { |
| 21 | + private readonly Repository repository; |
| 22 | + |
| 23 | + public BaseController(Repository repository) |
| 24 | + { |
| 25 | + this.repository = repository; |
| 26 | + } |
| 27 | + |
| 28 | + [HttpGet] |
| 29 | + public ActionResult Get() |
| 30 | + { |
| 31 | + |
| 32 | + return Ok(new |
| 33 | + { |
| 34 | + status = 200, |
| 35 | + data = repository.Get(), |
| 36 | + message = "Data berhasil Di tampilkan" |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + [HttpGet("{key}")] |
| 41 | + public ActionResult Get(Key key) |
| 42 | + { |
| 43 | + if (repository.Get(key) != null) |
| 44 | + { |
| 45 | + |
| 46 | + //return Ok(personRepository.Get(NIK)); |
| 47 | + return Ok(new |
| 48 | + { |
| 49 | + status = HttpStatusCode.OK, |
| 50 | + data = repository.Get(key), |
| 51 | + message = "Data berhasil Di tampilkan" |
| 52 | + }); |
| 53 | + } |
| 54 | + return NotFound(new |
| 55 | + { |
| 56 | + status = HttpStatusCode.NotFound, |
| 57 | + message = "Data dengan ID/NIK Tersebut Tidak Ditemukan" |
| 58 | + //return Ok(personRepository.Get(NIK)); |
| 59 | + }); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + [HttpPost] |
| 64 | + public ActionResult Insert(Entity entity) |
| 65 | + { |
| 66 | + if (repository.Insert(entity) != 0) |
| 67 | + { |
| 68 | + return Ok(new |
| 69 | + { |
| 70 | + status = HttpStatusCode.OK, |
| 71 | + message = "Data berhasil Di Tambahkan" |
| 72 | + }); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + return BadRequest(new |
| 77 | + { |
| 78 | + status = HttpStatusCode.BadRequest, |
| 79 | + message = "Gagal menambahkan data, periksa kembali" |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + [HttpPut] |
| 85 | + public ActionResult Update(Entity entity) |
| 86 | + { |
| 87 | + try |
| 88 | + { |
| 89 | + if (repository.Update(entity) != 0) |
| 90 | + { |
| 91 | + //return Ok("Data Berhasil di Update"); |
| 92 | + return Ok(new |
| 93 | + { |
| 94 | + status = HttpStatusCode.OK, |
| 95 | + message = "Data berhasil Di Update" |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | + catch (Exception e) |
| 100 | + { |
| 101 | + Console.WriteLine(e.Message); |
| 102 | + } |
| 103 | + return NotFound(new |
| 104 | + { |
| 105 | + status = HttpStatusCode.NotFound, |
| 106 | + message = "Data dengan ID/NIK tersebut Tidak Ditemukan" |
| 107 | + |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + [HttpDelete("{key}")] |
| 112 | + public ActionResult Delete(Key key) |
| 113 | + { |
| 114 | + if (repository.Get(key) != null) |
| 115 | + { |
| 116 | + return Ok(new |
| 117 | + { |
| 118 | + status = HttpStatusCode.OK, |
| 119 | + data = repository.Get(key), |
| 120 | + deletedata = repository.Delete(key), |
| 121 | + message = "Data berhasil Di Hapus" |
| 122 | + }); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + return NotFound(new |
| 127 | + { |
| 128 | + status = HttpStatusCode.NotFound, |
| 129 | + message = "Data dengan ID/NIK tersebut Tidak Ditemukan" |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | +} |
0 commit comments