0% found this document useful (0 votes)
21 views9 pages

Index: Ienumerable Customer

Uploaded by

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

Index: Ienumerable Customer

Uploaded by

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

Index

@model IEnumerable<Customer>
@{
ViewBag.Title = "Quản lý khách hàng";
}
<div class="box box-primary">
<div class="box-body">
<!--Form để nhập thông tin tìm kiếm -->
<form id="formSearch" action="~/Customer" method="get">
<div class="input-group">

<input name="searchValue"
type="text"
class="form-control"
placeholder="Nhập tên khách hàng cần tìm"
autofocus />

<div class="input-group-btn">
<button class="btn btn-info" type="submit">
<i class="fa fa-search"></i>
</button>

<a href="~/Customer/Create" class="btn btn-primary" style="margin-left:5px">


<i class="fa fa-plus"></i> Bổ sung
</a>
</div>
</div>
</form>
<!--Hiển thị dữ liệu -->
<p style="margin-top:10px">
có <strong>@ViewBag.RowCount</strong> khách hàng trong tổng số <strong>@ViewBag.PageCount</strong>
trang.
</p>
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<thead>
<tr class="bg-primary">
<th>Tên khách hàng</th>
<th>Tên giao dịch</th>
<th>Điện thoại</th>
<th>Email</th>
<th>Địa chỉ</th>
<th>Tỉnh thành</th>
<th>Bị khoá ?</th>
<th style="width:80px"></th>
</tr>
</thead>

<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.CustomerName</td>
<td>@item.ContactName</td>
<td>@item.Phone</td>
<td>@item.Email</td>
<td>@item.Address</td>
<td>@item.Province</td>
<td></td>
<td class="text-right">
<a href="~/Customer/Edit/@item.CustomerID" class="btn btn-xs btn-info">
<i class="fa fa-edit"></i>
</a>
<a href="~/Customer/Delete/@item.CustomerID class="btn btn-xs btn-danger">
<i class="fa fa-remove"></i>
</a>
</td>

</tr>
}

</tbody>
</table>
<div class="text-center">
<ul class="pagination">
@for (int p = 1; p <= ViewBag.PageCount; p++)
{
if (p == ViewBag.Page)
{
<li class="active"><a href="#">@p</a></li>
}
else
{
<li><a href="~Cutomer?page=@p&[email protected]">@p</a></li>
}
}

</ul>
</div>
</div>
</div>

Customer controller
using Microsoft.AspNetCore.Mvc;
using SV21T1020398.BusinessLayers;
using System.Buffers;

namespace SV21T1020398.Wed.Controllers
{
public class CustomerController : Controller
{
const int PAGE_SIZE = 20;

public IActionResult Index(int page = 1, string searchValue = "")


{

int rowCount = 0;
var data = CommonDataService.ListOfCustomers(out rowCount, page, PAGE_SIZE, searchValue ?? "");

int pageCount;
pageCount = rowCount/PAGE_SIZE;
if (rowCount % PAGE_SIZE > 0)
pageCount += 1;

ViewBag.Page = page;
ViewBag.PageCount = pageCount;
ViewBag.RowCount = rowCount;
ViewBag.SearchValue = searchValue;
return View(data);
}

public IActionResult Create()


{
ViewBag.Title = "Bổ sung khách hàng";

return View("Edit");
}

public IActionResult Edit(int id = 0)


{
ViewBag.Title = "Cập nhật thông tin khách hàng";

return View();
}

public IActionResult Delete(int id = 0)


{
ViewBag.Title = "Xóa Khách Hàng";
return View();

}
}
}

Business:

1.commonDataService
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Core;
using SV21T1020398.BusinessLayers;
using SV21T1020398.DataLayers;
using SV21T1020398.DomainModels;

namespace SV21T1020398.BusinessLayers

{
public class CommonDataService
{
static readonly ICommonDAL<Province> provinceDB;
static readonly ICommonDAL<Customer> customerDB;

static CommonDataService()
{
provinceDB = new DataLayers.SQLServer.ProvinceDAL(Configuration.ConnectionString);
customerDB = new DataLayers.SQLServer.CustomerDAL(Configuration.ConnectionString);
}
public static List<Province> ListOfProvinces()
{
return provinceDB.List().ToList();
}
/// <summary>
/// Danh sách khách hàng,( tìm kiếm , phân trang)
/// </summary>
/// <param name="rowCount"></param>
/// <param name="page"></param>
/// <param name="pageSize"></param>
/// <param name="searchValue"></param>
/// <returns></returns>
public static List<Customer> ListOfCustomers(out int rowCount, int page = 1, int pageSize = 0, string searchValue =
"")
{
rowCount = customerDB.Count(searchValue);
return customerDB.List(page, pageSize, searchValue).ToList();
}
public static List<Customer> ListOfCustomers(string searchValue = "")
{
return customerDB.List(1, 0, searchValue).ToList();
}

}
}

2.configuration

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SV21T1020398.BusinessLayers
{
/// <summary>
/// Cấu hình cho tầng Business
/// </summary>
public static class Configuration
{
public static string ConnectionString { get; private set; } = "";

/// <summary>
/// Khởi tạo cấu hình
/// </summary>
/// <param name="connectionString"></param>
public static void Initialize(string connectionString)
{

ConnectionString = connectionString;
}
}
}
Datalayer:

1. ICommonDAL

using System.Buffers;

namespace SV21T1020398.DataLayers
{
public interface ICommonDAL<T> where T : class
{
IList<T> List(int page = 1, int pageSize = 0, string searchValues = "");

int Count(string searchValues = "");

T? Get(int id);

int Add(T data);

bool Update(T data);

bool Delete(int id);

bool InUsed(int id);


}
}

2. folder: sql server

a. _BaseDAL:

using Microsoft.Data.SqlClient;

namespace SV21T1020398.DataLayers.SQLServer
{
public abstract class _BaseDAL
{
protected string _connectionString = "";

public _BaseDAL(string connectionString)


{
_connectionString = connectionString;
}

protected SqlConnection OpenConnection()


{
SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
return connection;
}
}
}

b. customerDAL

using Dapper;
using SV21T1020398.DataLayers.SQLServer;
using SV21T1020398.DataLayers;
using SV21T1020398.DomainModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SV21T1020398.DataLayers.SQLServer
{
public class CustomerDAL : _BaseDAL, ICommonDAL<Customer>
{
public CustomerDAL(string connectionString) : base(connectionString)
{
}

public int Add(Customer data)


{
throw new NotImplementedException();
}

public int Count(string searchValue = "")


{
int count = 0;
using (var connection = OpenConnection())
{
var sql = @"select count(*)
from Customers
where CustomerName like @searchValue or ContactName like @searchValue";
var parameters = new
{
searchValue = $"%{searchValue}%"
};
count = connection.ExecuteScalar<int>(sql: sql, param: parameters, commandType: CommandType.Text);
connection.Close();
}
return count;

public Customer? Get(int id)


{
throw new NotImplementedException();
}

public bool InUsed(int id)


{
throw new NotImplementedException();
}

public IList<Customer> List(int page = 1, int pagesize = 10, string searchValue = "")
{
List<Customer> data = new List<Customer>();
using (var connection = OpenConnection())
{
var sql = @"select*
from(
select *,
ROW_NUMBER()over (order by CustomerName) as RowNumber
from Customers
where CustomerName like @searchValue or ContactName like @searchValue
) as t
where @pageSize=0
or RowNumber between (@page-1) * @pageSize +1 and (@page *@pageSize)
";
var parameters = new
{
page = page,
pagesize = pagesize,
searchValue = $"%{searchValue}%"
};
data = connection.Query<Customer>(sql: sql, param: parameters, commandType:
CommandType.Text).ToList();
}
return data;
}

public int Update(Customer data)


{
throw new NotImplementedException();
}

bool ICommonDAL<Customer>.Delete(int id)


{
throw new NotImplementedException();
}

bool ICommonDAL<Customer>.Update(Customer data)


{
throw new NotImplementedException();
}
}
}

c. provincedal

using SV21T1020398.DomainModels;
using Dapper;
using Microsoft.Data.SqlClient;
using System.Data;

namespace SV21T1020398.DataLayers.SQLServer
{
public class ProvinceDAL : _BaseDAL, ICommonDAL<Province>
{
public ProvinceDAL(string connectionString) : base(connectionString)
{
}

public int Add(Province data)


{
throw new NotImplementedException();
}
public int Count(string searchValues = "")
{
throw new NotImplementedException();
}

public bool Delete(int id)


{
throw new NotImplementedException();
}

public Province? Get(int id)


{
throw new NotImplementedException();
}

public bool InUsed(int id)


{
throw new NotImplementedException();
}

public IList<Province> List(int page = 1, int pageSize = 0, string searchValues = "")


{
List<Province> data = new List<Province>();
using (var connection = OpenConnection())
{
var sql = @"SELECT * FROM Province";
data = connection.Query<Province>(sql: sql, commandType: CommandType.Text).ToList();
connection.Close();
}
return data;
}

public bool Update(Province data)


{
throw new NotImplementedException();
}
}
}

Domainmolder:

namespace SV21T1020398.DomainModels
{
public class Province
{

public string ProvinceName { get; set; } = string.Empty;

}
}

Program

var builder = WebApplication.CreateBuilder(args);


// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.


if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

string connectionString = @"server=DESKTOP-B3MTRBN;user


id=sa;password=123456;database=LiteCommerceDB;TrustServerCertificate=true";
SV21T1020398.BusinessLayers.Configuration.Initialize(connectionString);
app.Run();

You might also like