using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; using CertAuth.Data; using CertAuth.Domain; namespace CertAuthAdmin.Controllers { [Route("api/[controller]/[action]")] public class TableDtlController : Controller { private CertAuthContext _context; public TableDtlController(CertAuthContext context) { _context = context; } [HttpGet] public async Task Get(DataSourceLoadOptions loadOptions) { var tabledtls = _context.TableDtl.Select(i => new { i.Id, i.TableCode, i.DetailCode, i.CodeDescShort, i.CodeDescLong, i.Status, i.TimeStamp }); // If you work with a large amount of data, consider specifying the PaginateViaPrimaryKey and PrimaryKey properties. // In this case, keys and data are loaded in separate queries. This can make the SQL execution plan more efficient. // Refer to the topic https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/336. // loadOptions.PrimaryKey = new[] { "Id" }; // loadOptions.PaginateViaPrimaryKey = true; return Json(await DataSourceLoader.LoadAsync(tabledtls, loadOptions)); } [HttpGet] public object TableDetails(int id, DataSourceLoadOptions loadOptions) { var dtls = _context.TableDtl.Where(dtl => dtl.Id == id); return DataSourceLoader.Load(dtls, loadOptions); } [HttpGet] //public object ActiveStatus(string TableCode, string Status, DataSourceLoadOptions loadOptions) public object ActiveStatus(string TblCd, DataSourceLoadOptions loadOptions) { //var dtls = _context.TableDtl.Where(e => e.TableCode == TableCode && e.Status == Status).Select(e => new { e.DetailCode, e.CodeDescShort }); //var dtls = _context.TableDtl.Where(e => e.TableCode == "Status" && e.Status == "Active"); var dtls = _context.TableDtl.Where(e => e.TableCode == TblCd && e.Status == "A"); return DataSourceLoader.Load(dtls, loadOptions); } [HttpPost] public async Task Post(string values) { var model = new TableDtl(); var valuesDict = JsonConvert.DeserializeObject(values); PopulateModel(model, valuesDict); if(!TryValidateModel(model)) return BadRequest(GetFullErrorMessage(ModelState)); var result = _context.TableDtl.Add(model); await _context.SaveChangesAsync(); return Json(result.Entity.Id); } [HttpPut] public async Task Put(int key, string values) { var model = await _context.TableDtl.FirstOrDefaultAsync(item => item.Id == key); if(model == null) return StatusCode(409, "TableDtl not found"); var valuesDict = JsonConvert.DeserializeObject(values); PopulateModel(model, valuesDict); if(!TryValidateModel(model)) return BadRequest(GetFullErrorMessage(ModelState)); _context.TableDtl.Update(model); await _context.SaveChangesAsync(); return Ok(); } [HttpDelete] public async Task Delete(int key) { var model = await _context.TableDtl.FirstOrDefaultAsync(item => item.Id == key); _context.TableDtl.Remove(model); await _context.SaveChangesAsync(); } private void PopulateModel(TableDtl model, IDictionary values) { string ID = nameof(TableDtl.Id); string TABLE_CODE = nameof(TableDtl.TableCode); string DETAIL_CODE = nameof(TableDtl.DetailCode); string CODE_DESC_SHORT = nameof(TableDtl.CodeDescShort); string CODE_DESC_LONG = nameof(TableDtl.CodeDescLong); string STATUS = nameof(TableDtl.Status); string TIME_STAMP = nameof(TableDtl.TimeStamp); if(values.Contains(ID)) { model.Id = Convert.ToInt32(values[ID]); } if(values.Contains(TABLE_CODE)) { model.TableCode = Convert.ToString(values[TABLE_CODE]); } if(values.Contains(DETAIL_CODE)) { model.DetailCode = Convert.ToString(values[DETAIL_CODE]); } if(values.Contains(CODE_DESC_SHORT)) { model.CodeDescShort = Convert.ToString(values[CODE_DESC_SHORT]); } if(values.Contains(CODE_DESC_LONG)) { model.CodeDescLong = Convert.ToString(values[CODE_DESC_LONG]); } if(values.Contains(STATUS)) { model.Status = Convert.ToString(values[STATUS]); } if(values.Contains(TIME_STAMP)) { model.TimeStamp = Convert.ToDateTime(values[TIME_STAMP]); } } private string GetFullErrorMessage(ModelStateDictionary modelState) { var messages = new List(); foreach(var entry in modelState) { foreach(var error in entry.Value.Errors) messages.Add(error.ErrorMessage); } return String.Join(" ", messages); } } }