CoreAPI
CoreAPI
cs--------------------------
using CoreApp.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoreApp.Service
{
//Implemented Interface
public class Product: IProduct
{
// 1. Created Constructor
await _esankbakeryContext.SaveChangesAsync();
}
//_esankbakeryContext.TblProduct.Update(product);
//await _esankbakeryContext.SaveChangesAsync();
//return product;
}
catch (Exception ex)
{
//return null;
}
}
}
-----------------------------------PRODUCT CONTROLLER----------------------------
using CoreApp.Model;
using CoreApp.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace CoreApp.Controllers
{
[ApiController]
public class ProductController : ControllerBase
{
private readonly IProduct _Product;
public ProductController(IProduct Product)
{
_Product = Product;
}
[Route("api/[controller]/get")]
[HttpGet]
public async Task<IActionResult> GetDeptList()
{
var result = await _Product.GetDeptList();
return Ok(result);
}
[Route("api/[controller]/add")]
[HttpPost]
public async Task<IActionResult> AddProduct(TblProduct product)
{
try
{
var result = await _Product.AddProduct(product);
return Ok(result);
}
catch(Exception ex)
{
return StatusCode(500);
}
}
[Route("api/[controller]/delete/{Id}")]
[HttpDelete]
public async Task<IActionResult> DeleteProduct(int Id)
{
try
{
var result = await _Product.DeleteProduct(Id);
return Ok(result);
}
catch (Exception)
{
return StatusCode(500);
}
}
[Route("api/[controller]/getbyid/{pId}")]
[HttpGet]
public async Task<IActionResult> GetProductByProductId(int pId)
{
try
{
var result = await _Product.GetProductByProductId(pId);
return Ok(result);
}
catch (Exception)
{
return StatusCode(500);
}
}
[Route("api/[controller]/edit/{pId}")]
[HttpPut]
public async Task<IActionResult> UpdateProduct(int pId,TblProduct product)
{
try
{
await _Product.UpdateProduct(pId,product);
return Ok();
}
catch (Exception ex)
{
return StatusCode(500);
}
}
}
}
----------------Context------------------------------------
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace CoreApp.Model
{
public partial class esankbakeryContext : DbContext
{
public esankbakeryContext()
{
}
entity.ToTable("tblBooking");
entity.Property(e => e.Address)
.IsRequired()
.HasMaxLength(500);
modelBuilder.Entity<TblCategory>(entity =>
{
entity.HasKey(e => e.CategoryId);
entity.ToTable("tblCategory");
modelBuilder.Entity<TblCustomer>(entity =>
{
entity.HasKey(e => e.CustomerId);
entity.ToTable("tbl_Customer");
modelBuilder.Entity<TblCustomerEvent>(entity =>
{
entity.HasKey(e => e.EventId);
entity.ToTable("tblCustomerEvent");
modelBuilder.Entity<TblOrder>(entity =>
{
entity.HasKey(e => e.OrderId);
entity.ToTable("tblOrder");
modelBuilder.Entity<TblProduct>(entity =>
{
entity.HasKey(e => e.ProductId);
entity.ToTable("tblProduct");
modelBuilder.Entity<Tblcontact>(entity =>
{
entity.HasKey(e => e.ContactId);
entity.ToTable("tblcontact");
modelBuilder.Entity<Tblfeedback>(entity =>
{
entity.HasKey(e => e.CustomerId);
entity.ToTable("tblfeedback");
OnModelCreatingPartial(modelBuilder);
}
------------------------------ Startup.cs-----------------------------------------
using CoreApp.Model;
using CoreApp.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoreApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services
to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<esankbakeryContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ConnectionString")));
services.AddTransient<IProduct, Product>();
}
// This method gets called by the runtime. Use this method to configure the
HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
-----------------------------IProduct.cs-----------------------------
using CoreApp.Model;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CoreApp.Service
{
public interface IProduct
{
Task<IEnumerable<TblProduct>> GetDeptList();
Task<TblProduct> AddProduct(TblProduct product);
}
}