VGetAllCategoriesController.cs

<< >>

Navigation:  CodeStencil > Troubleshooting > Errors In Generated Files >

VGetAllCategoriesController.cs

 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Globalization;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Mvc.ModelBinding;

using Microsoft.EntityFrameworkCore;

using DevExtreme.AspNet.Data;

using DevExtreme.AspNet.Mvc;

using Newtonsoft.Json;

using Project16.Models.EF;

 

namespace Project16.Controllers

{

   [Route("api/[controller]/[action]")]

  public class VGetAllCategoriesController : Controller

   {

      private readonly DevAVContext _context;

 

      public VGetAllCategoriesController(DevAVContext context) {

           _context = context;

       }

 

       [HttpGet]

      public async Task<IActionResult> Get(DataSourceLoadOptions loadOptions) {

          var vgetallcategories = _context.VGetAllCategory.Select(i => new {

                i.ParentProductCategoryName,

                i.ProductCategoryName,

                i.ProductCategoryID

         });

       

      // 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(vgetallcategories, loadOptions));

       }

 

 

       [HttpPost]

      public async Task<IActionResult> Post(string values) {

          var model = new VGetAllCategory();

          var valuesDict = JsonConvert.DeserializeObject<IDictionary>(values);

           PopulateModel(model, valuesDict);

 

          if(!TryValidateModel(model))

              return BadRequest(GetFullErrorMessage(ModelState));

 

          var result = _context.VGetAllCategory.Add(model);

          await _context.SaveChangesAsync();

 

 

       }

 

       [HttpPut]

      public async Task<IActionResult> Put(long key, string values) {

          var model = await _context.VGetAllCategory.FirstOrDefaultAsync(item => item.[%CS_PRIMARY_KEY%] == key);

          if(model == null)

              return StatusCode(409, "Object not found");

 

          var valuesDict = JsonConvert.DeserializeObject<IDictionary>(values);

           PopulateModel(model, valuesDict);

 

          if(!TryValidateModel(model))

              return BadRequest(GetFullErrorMessage(ModelState));

 

          await _context.SaveChangesAsync();

          return Ok();

       }

 

       [HttpDelete]

      public async Task Delete(long key) {

          var model = await _context.VGetAllCategory.FirstOrDefaultAsync(item => item.[%CS_PRIMARY_KEY%] == key);

 

           _context.VGetAllCategory.Remove(model);

          await _context.SaveChangesAsync();

       }

 

 

 

      private void PopulateModel(VGetAllCategory model, IDictionary values) {

          string PARENT_PRODUCT_CATEGORY_NAME = nameof(VGetAllCategory.ParentProductCategoryName);

          string PRODUCT_CATEGORY_NAME = nameof(VGetAllCategory.ProductCategoryName);

          string PRODUCT_CATEGORY_ID = nameof(VGetAllCategory.ProductCategoryID);

       

          if(values.Contains(PARENT_PRODUCT_CATEGORY_NAME)) {

               model.ParentProductCategoryName = Convert.ToString(values[PARENT_PRODUCT_CATEGORY_NAME]);

           }

       

          if(values.Contains(PRODUCT_CATEGORY_NAME)) {

               model.ProductCategoryName = values[PRODUCT_CATEGORY_NAME] != null ? Convert.ToString(values[PRODUCT_CATEGORY_NAME]) : (string?)null;

           }

       

          if(values.Contains(PRODUCT_CATEGORY_ID)) {

               model.ProductCategoryID = values[PRODUCT_CATEGORY_ID] != null ? Convert.ToInt32(values[PRODUCT_CATEGORY_ID]) : (int?)null;

           }

       

       }

 

 

      private string GetFullErrorMessage(ModelStateDictionary modelState) {

          var messages = new List<string>();

 

          foreach(var entry in modelState) {

              foreach(var error in entry.Value.Errors)

                   messages.Add(error.ErrorMessage);

           }

 

          return String.Join(" ", messages);

       }

 

   }

}