[go: up one dir, main page]

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

Repository Và Factory

Uploaded by

monpen.nnt
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)
7 views10 pages

Repository Và Factory

Uploaded by

monpen.nnt
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/ 10

Tạo class SanPhamRepository ở Model

using Microsoft.EntityFrameworkCore;

using PayPalCheckoutSdk.Orders;

using QLThuocDAPM.Data;

namespace QLThuocDAPM.Models

public class SanPhamRepository : ISanPhamRepository

private readonly QlthuocDapm6Context _context;

public SanPhamRepository(QlthuocDapm6Context context)

_context = context;

public async Task<IEnumerable<SanPham>> GetAllAsync()

return await _context.SanPhams

.Include(s => s.MaBenhNavigation)

.Include(s => s.MaDmNavigation)

.Include(s => s.MaGiamGiaNavigation)

.Include(s => s.MaNhaCungCapNavigation)

.ToListAsync();

public async Task<SanPham> GetByIdAsync(int id)

{
return await _context.SanPhams

.Include(s => s.MaBenhNavigation)

.Include(s => s.MaDmNavigation)

.Include(s => s.MaGiamGiaNavigation)

.Include(s => s.MaNhaCungCapNavigation)

.FirstOrDefaultAsync(sp => sp.MaSp == id);

public async Task<IEnumerable<SanPham>> GetPagedAsync(int pageNumber, int


pageSize)

return await _context.SanPhams

.Skip((pageNumber - 1) * pageSize)

.Take(pageSize)

.ToListAsync();

public async Task AddAsync(SanPham sanPham)

_context.SanPhams.Add(sanPham);

await _context.SaveChangesAsync();

public async Task UpdateAsync(SanPham sanPham)

_context.SanPhams.Update(sanPham);

await _context.SaveChangesAsync();

}
public async Task DeleteAsync(int id)

var sanPham = await _context.SanPhams.FindAsync(id);

if (sanPham != null)

_context.SanPhams.Remove(sanPham);

await _context.SaveChangesAsync();

public async Task<bool> ExistsAsync(int id)

return await _context.SanPhams.AnyAsync(e => e.MaSp == id);

Tạo class IsanPhamRepository ở Model

using QLThuocDAPM.Data;

namespace QLThuocDAPM.Models

public interface ISanPhamRepository

Task<IEnumerable<SanPham>> GetAllAsync();

Task<SanPham> GetByIdAsync(int id);

Task<IEnumerable<SanPham>> GetPagedAsync(int pageNumber, int pageSize);

Task AddAsync(SanPham sanPham);


Task UpdateAsync(SanPham sanPham);

Task DeleteAsync(int id);

Task<bool> ExistsAsync(int id);

Ở phần đầu SanPhamController

private readonly QlthuocDapm6Context _context;

private readonly ISanPhamRepository _sanPhamRepository;

private readonly ISanPhamFactory _sanPhamFactory;

public SanPhamsController(QlthuocDapm6Context context, ISanPhamFactory


sanPhamFactory, ISanPhamRepository sanPhamRepository)

_context = context;

_sanPhamFactory = sanPhamFactory;

_sanPhamRepository = sanPhamRepository;

public async Task<IActionResult> Index(int pageNumber = 1)

int pageSize = 5; // Số sản phẩm trên mỗi trang

var sanPhams = await _sanPhamRepository.GetPagedAsync(pageNumber, pageSize);

int totalProducts = (await _sanPhamRepository.GetAllAsync()).Count();

int totalPages = (int)Math.Ceiling(totalProducts / (double)pageSize);

ViewBag.CurrentPage = pageNumber;

ViewBag.TotalPages = totalPages;
return View(sanPhams);

// GET: Admin/SanPhams/Details/5

public async Task<IActionResult> Details(int id)

var sanPham = await _sanPhamRepository.GetByIdAsync(id);

if (sanPham == null)

return NotFound();

return View(sanPham);

Tạo SanPhamFactory và IsanPhamFactory

SanPhamFactory

using QLThuocDAPM.Data;

namespace QLThuocDAPM.Models

public class SanPhamFactory : ISanPhamFactory

public SanPham CreateSanPham(int maSp, string tenSp, int maBenh, int


maNhaCungCap, int maGiamGia,

string thanhPhan, string congDung, string cachDung, string doiTuongSuDung,

string tacDungPhu, double giaTien, double? giaSauGiam, string donVi,

DateOnly ngaySanXuat, string noiSanXuat, DateOnly? hanSuDung, string chiTietSp,


int maDm, int? soLuong, int? soLuongMua,int? soBinhLuan, string hinhAnh1, string
hinhAnh2,

string hinhAnh3, string hinhAnh4)

return new SanPham

MaSp = maSp,

TenSp = tenSp,

MaBenh = maBenh,

MaNhaCungCap = maNhaCungCap,

MaGiamGia = maGiamGia,

ThanhPhan = thanhPhan,

Congdung = congDung,

Cachdung = cachDung,

Doituongsudung = doiTuongSuDung,

Tacdungphu = tacDungPhu,

GiaTien = giaTien,

GiaSauGiam = giaSauGiam,

DonVi = donVi,

Ngaysanxuat = ngaySanXuat,

Noisanxuat = noiSanXuat,

HansuDung = hanSuDung,

ChitietSp = chiTietSp,

MaDm = maDm,

SoLuong = soLuong,

SoLuongMua = soLuongMua,

SoBinhLuan = soBinhLuan,

HinhAnh1 = hinhAnh1,
HinhAnh2 = hinhAnh2,

HinhAnh3 = hinhAnh3,

HinhAnh4 = hinhAnh4

};

ISanPhamFactory

using QLThuocDAPM.Data;

namespace QLThuocDAPM.Models

public interface ISanPhamFactory

SanPham CreateSanPham(int maSp, string tenSp, int maBenh, int maNhaCungCap, int
maGiamGia,

string thanhPhan, string congDung, string cachDung, string doiTuongSuDung,

string tacDungPhu, double giaTien, double? giaSauGiam, string donVi,

DateOnly ngaySanXuat, string noiSanXuat, DateOnly? hanSuDung, string chiTietSp,

int maDm, int? soLuong, int? soLuongMua, int? soBinhLuan, string hinhAnh1, string
hinhAnh2,

string hinhAnh3, string hinhAnh4);

Create ở SanPhamController

[HttpPost]

[ValidateAntiForgeryToken]

public async Task<IActionResult> Create(


[Bind("MaSp,TenSp,MaBenh,MaNhaCungCap,MaGiamGia,ThanhPhan,Congdung,Cachdung,D
oituongsudung,Tacdungphu,GiaTien,DonVi,Ngaysanxuat,Noisanxuat,HansuDung,ChitietSp,M
aDm,SoLuong,SoLuongMua,,SoBinhLuan,HinhAnh1,HinhAnh2,HinhAnh3,HinhAnh4")]

SanPham sanPham, IFormFile file1, IFormFile file2, IFormFile file3, IFormFile file4)

if (ModelState.IsValid)

// Kiểm tra xem tất cả file ảnh có được upload hay không

if (file1 != null && file2 != null && file3 != null && file4 != null)

// Lưu file ảnh vào thư mục wwwroot/images

var fileNames = new List<string> { file1.FileName, file2.FileName, file3.FileName,


file4.FileName };

var filePaths = fileNames.Select(name =>


Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", name)).ToList();

for (int i = 0; i < fileNames.Count; i++)

using (var stream = new FileStream(filePaths[i], FileMode.Create))

await new List<IFormFile> { file1, file2, file3, file4 }[i].CopyToAsync(stream);

// Tạo đối tượng sản phẩm bằng Factory

var factory = new SanPhamFactory();

var newSanPham = factory.CreateSanPham(

sanPham.MaSp, sanPham.TenSp, sanPham.MaBenh, sanPham.MaNhaCungCap,


sanPham.MaGiamGia,
sanPham.ThanhPhan, sanPham.Congdung, sanPham.Cachdung,
sanPham.Doituongsudung,

sanPham.Tacdungphu, sanPham.GiaTien, sanPham.GiaSauGiam, sanPham.DonVi,

sanPham.Ngaysanxuat, sanPham.Noisanxuat, sanPham.HansuDung,


sanPham.ChitietSp,

sanPham.MaDm, sanPham.SoLuong, sanPham.SoLuongMua,


sanPham.SoBinhLuan,

"/images/" + fileNames[0], "/images/" + fileNames[1], "/images/" + fileNames[2],


"/images/" + fileNames[3]

);

_context.Add(newSanPham);

await _context.SaveChangesAsync();

return RedirectToAction(nameof(Index));

ViewData["MaBenh"] = new SelectList(_context.Benhs, "MaBenh", "MaBenh",


sanPham.MaBenh);

ViewData["MaDm"] = new SelectList(_context.DanhMucs, "MaDm", "MaDm",


sanPham.MaDm);

ViewData["MaGiamGia"] = new SelectList(_context.GiamGia, "MaGiamGia",


"MaGiamGia", sanPham.MaGiamGia);

ViewData["MaNhaCungCap"] = new SelectList(_context.NhaCungCaps,


"MaNhaCungCap", "MaNhaCungCap", sanPham.MaNhaCungCap);

return View(sanPham);

Bỏ ở Program
builder.Services.AddScoped<ISanPhamFactory, SanPhamFactory>();

builder.Services.AddScoped<ISanPhamRepository, SanPhamRepository>();

You might also like