using-System
using-System
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Inventory_Dapper.Model;
using Inventory_Dapper.Repository;
namespace Inventory_Dapper.Services
{
public class CategoryService
{
CategoryRepository _categoryRepository;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Inventory_Dapper.Model;
using MyFirstDapper.Repository;
namespace Inventory_Dapper.Repository
{
public class CategoryRepository : GenericRepository<CategoryModel>
{
}
}
using Dapper;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstDapper.Repository
{
public class GenericRepository<T> : IgenericRepository<T> where T : class
{
IDbConnection _connection;
readonly string connectionString = "Server=LAPTOP-0QGDR041;
Database=InventoryDb; Trusted_Connection=True; MultipleActiveResultSets=True;";
public GenericRepository()
{
_connection = new SqlConnection(connectionString);
}
int affectedRows = 0;
try
{
var properties = typeof(T).GetProperties()
.Where(p => p.GetCustomAttribute<KeyAttribute>() == null) // Exclude key
column from SET
.ToList();
if (!properties.Any())
throw new InvalidOperationException("No columns to update. Ensure the
entity has properties other than the key.");
try
{
var sql = $"DELETE FROM {tableName} WHERE {keyColumn} =
@{keyColumn}";
var result = _connection.Execute(sql, entity);
return result > 0;
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting entity: {ex.Message}");
return false;
}
}
//Get all Data
public IEnumerable<T> GetAll()
{
string tableName = GetTableName();
string query = $"SELECT * FROM {tableName}";
return _connection.Query<T>(query);
}
return tableName;
}
//Function Column
public string GetColumnNames(bool excludeKey = false)
{
string columnNames = "";
var type = typeof(T);
var columns = string.Join(", ", type.GetProperties()
.Where(p => !excludeKey || !p.IsDefined(typeof(KeyAttribute)))
.Select(p =>
{
var columnAttr = p.GetCustomAttribute<ColumnAttribute>();
return columnAttr != null ? columnAttr.Name : p.Name;
}
));
return columns;
return values;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstDapper.Repository
{
public interface IgenericRepository<T> where T : class
{
T GetById(int id);
using Inventory_Dapper.Model;
using Inventory_Dapper.Repository;
using Inventory_Dapper.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Inventory_SystemAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CategoryController : ControllerBase
{
CategoryRepository _categoryRepository;
[HttpGet]
public ActionResult<IEnumerable<CategoryModel>> GetAllCategories()
{
CategoryService _categoryService = new CategoryService();
var categories = _categoryService.GetAllCategories();
return Ok(categories);
}
[HttpPost]
public bool AddCategory(CategoryModel category)
{
CategoryService _categoryService = new CategoryService();
_categoryService.AddCategory(category);
return (true);
}
[HttpPut]
public bool UpdateCategory(CategoryModel category)
{
CategoryService _categoryService = new CategoryService();
_categoryService.UpdateCategory(category);
return (true);
}
[HttpDelete]
public bool DeleteCategory(CategoryModel category) {
CategoryService _categoryService = new CategoryService();
_categoryService.DeleteCategory(category);
return (true);
}
}
}