0% found this document useful (0 votes)
11 views10 pages

CoreAPI

The document outlines the implementation of a product management system using ASP.NET Core and Entity Framework. It includes a Product service class that handles CRUD operations for products, a Product controller for API endpoints, and a DbContext class for database interactions. Additionally, it defines an interface for product operations and a Startup class for configuring services and middleware in the application.

Uploaded by

Payal Rahangdale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

CoreAPI

The document outlines the implementation of a product management system using ASP.NET Core and Entity Framework. It includes a Product service class that handles CRUD operations for products, a Product controller for API endpoints, and a DbContext class for database interactions. Additionally, it defines an interface for product operations and a Startup class for configuring services and middleware in the application.

Uploaded by

Payal Rahangdale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

-----------------------Product.

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

private readonly esankbakeryContext _esankbakeryContext;


public Product(esankbakeryContext esankbakeryContext)
{
_esankbakeryContext = esankbakeryContext;
}

public async Task<IEnumerable<TblProduct>> GetDeptList()


{
var DepList = await _esankbakeryContext.TblProduct.ToListAsync();
return DepList;
}

public async Task<TblProduct> AddProduct(TblProduct product)


{
_esankbakeryContext.TblProduct.Add(product);
await _esankbakeryContext.SaveChangesAsync();
return product;
}
public async Task<TblProduct> DeleteProduct(int Id)
{
var result = await _esankbakeryContext.TblProduct.FirstOrDefaultAsync(x
=> x.ProductId == Id);
if (result == null)
{
return null;
}
_esankbakeryContext.TblProduct.Remove(result);
_esankbakeryContext.SaveChangesAsync();
return result;
}

public async Task<TblProduct> GetProductByProductId(int pId)


{
var result = await _esankbakeryContext.TblProduct.FirstOrDefaultAsync(x
=> x.ProductId == pId);
if (result == null)
{
return null;
}
return result;
}
public async Task UpdateProduct(int pId,TblProduct product)
{
try
{

var result = await _esankbakeryContext.TblProduct.FindAsync(pId);


if(result != null)
{
//result.ProductId = product.ProductId;
result.ProductName = product.ProductName;
result.CategoryId = product.CategoryId;
result.Price = product.Price;
result.Unit = product.Unit;
result.ProductPhoto = product.ProductPhoto;
result.ManufacturingDate = product.ManufacturingDate;
result.ExpiryDate = product.ExpiryDate;

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()
{
}

public esankbakeryContext(DbContextOptions<esankbakeryContext> options)


: base(options)
{
}

public virtual DbSet<TblBooking> TblBooking { get; set; }


public virtual DbSet<TblCategory> TblCategory { get; set; }
public virtual DbSet<TblCustomer> TblCustomer { get; set; }
public virtual DbSet<TblCustomerEvent> TblCustomerEvent { get; set; }
public virtual DbSet<TblOrder> TblOrder { get; set; }
public virtual DbSet<TblProduct> TblProduct { get; set; }
public virtual DbSet<Tblcontact> Tblcontact { get; set; }
public virtual DbSet<Tblfeedback> Tblfeedback { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder


optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string,
you should move it out of source code. See http://go.microsoft.com/fwlink/?
LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Server=DESKTOP-HJIH5Q6\\
SQLEXPRESS;Database=esankbakery;Integrated Security=True;");
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)


{
modelBuilder.Entity<TblBooking>(entity =>
{
entity.HasKey(e => e.OrderId);

entity.ToTable("tblBooking");
entity.Property(e => e.Address)
.IsRequired()
.HasMaxLength(500);

entity.Property(e => e.City)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.DeliveryDate).HasColumnType("date");

entity.Property(e => e.MobileNo)


.IsRequired()
.HasMaxLength(20);

entity.Property(e => e.Name)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Note).HasMaxLength(50);

entity.Property(e => e.Price).HasColumnType("decimal(18, 2)");

entity.Property(e => e.Size).HasColumnType("decimal(18, 2)");

entity.Property(e => e.TotalPrice).HasColumnType("decimal(18, 2)");


});

modelBuilder.Entity<TblCategory>(entity =>
{
entity.HasKey(e => e.CategoryId);

entity.ToTable("tblCategory");

entity.Property(e => e.CategoryName)


.IsRequired()
.HasMaxLength(50);
});

modelBuilder.Entity<TblCustomer>(entity =>
{
entity.HasKey(e => e.CustomerId);

entity.ToTable("tbl_Customer");

entity.Property(e => e.Address)


.IsRequired()
.HasMaxLength(100);

entity.Property(e => e.City)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.CustomerName)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Email)


.IsRequired()
.HasMaxLength(50);
entity.Property(e => e.MobileNo)
.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Pincode)


.IsRequired()
.HasMaxLength(50);
});

modelBuilder.Entity<TblCustomerEvent>(entity =>
{
entity.HasKey(e => e.EventId);

entity.ToTable("tblCustomerEvent");

entity.Property(e => e.CustomerName)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.EventDate).HasColumnType("date");

entity.Property(e => e.EventType)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.MobileNo)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Photo)


.HasColumnName("photo")
.HasMaxLength(50);

entity.Property(e => e.WhatsAppNo)


.IsRequired()
.HasMaxLength(50);
});

modelBuilder.Entity<TblOrder>(entity =>
{
entity.HasKey(e => e.OrderId);

entity.ToTable("tblOrder");

entity.Property(e => e.Address).IsRequired();

entity.Property(e => e.Amount).HasColumnType("decimal(18, 0)");

entity.Property(e => e.Delivery).HasColumnType("date");

entity.Property(e => e.Flavour)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Img).HasMaxLength(50);

entity.Property(e => e.Mobile)


.IsRequired()
.HasMaxLength(20);

entity.Property(e => e.Name)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Note)


.IsRequired()
.HasMaxLength(10)
.IsFixedLength();

entity.Property(e => e.Size)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Type)


.IsRequired()
.HasMaxLength(50);
});

modelBuilder.Entity<TblProduct>(entity =>
{
entity.HasKey(e => e.ProductId);

entity.ToTable("tblProduct");

entity.Property(e => e.ExpiryDate).HasColumnType("date");

entity.Property(e => e.ManufacturingDate).HasColumnType("date");

entity.Property(e => e.Price).HasColumnType("decimal(18, 0)");

entity.Property(e => e.ProductName)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.ProductPhoto)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Size).HasColumnType("decimal(18, 0)");

entity.Property(e => e.Unit)


.IsRequired()
.HasMaxLength(10)
.IsFixedLength();
});

modelBuilder.Entity<Tblcontact>(entity =>
{
entity.HasKey(e => e.ContactId);

entity.ToTable("tblcontact");

entity.Property(e => e.Email)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Message).HasMaxLength(500);


entity.Property(e => e.MobileNo)
.IsRequired()
.HasMaxLength(20);

entity.Property(e => e.Name)


.IsRequired()
.HasMaxLength(50);
});

modelBuilder.Entity<Tblfeedback>(entity =>
{
entity.HasKey(e => e.CustomerId);

entity.ToTable("tblfeedback");

entity.Property(e => e.Address).HasMaxLength(500);

entity.Property(e => e.City)


.IsRequired()
.HasMaxLength(50);

entity.Property(e => e.Feedback).IsRequired();

entity.Property(e => e.Name)


.IsRequired()
.HasMaxLength(50);
});

OnModelCreatingPartial(modelBuilder);
}

partial void OnModelCreatingPartial(ModelBuilder 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;
}

public IConfiguration Configuration { get; }

// 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);

Task<TblProduct> DeleteProduct(int Id);

Task<TblProduct> GetProductByProductId(int pId);


Task UpdateProduct(int pId,TblProduct product);

}
}

You might also like