public async Task <IHttpActionResult> Update(int khachDatHangID, [FromBody] KhachDatHang khachDatHang) { if (khachDatHang.KhachDatHangID != khachDatHangID) { return(BadRequest("Id mismatch")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (var db = new ApplicationDbContext()) { db.Entry(khachDatHang).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ducEx) { bool exists = db.KhachDatHang.Count(o => o.KhachDatHangID == khachDatHangID) > 0; if (!exists) { return(NotFound()); } else { throw ducEx; } } return(Ok(khachDatHang)); } }
public ActionResult DatLich() { ViewBag.message = null; NguoiDung nguoiDung = new NguoiDung(); nguoiDung.SoDienThoai = Request.Form["sodienthoai"]; KhachDatHang khachDatHang = new KhachDatHang(); khachDatHang.NgayDat = Convert.ToDateTime(Request.Form["ngaydat"]); khachDatHang.GioDat = int.Parse(Request.Form["giodat"]); int gioDat = int.Parse(Request.Form["giodat"]); KhachDatHangService khachDatHangService = new KhachDatHangService(); int result = khachDatHangService.ThemKhachDatHang(khachDatHang, nguoiDung); if (result == 2) { ViewBag.message = "Đặt lịch thành công!"; return(RedirectToAction("LienHe", new { trangThai = result })); } else if (result == 1) { ViewBag.message = "Đặt lịch thành công!"; return(RedirectToAction("LienHe", new { trangThai = result })); } else { ViewBag.message = "Có lỗi xảy ra, vui lòng thử lại!"; return(View("Index")); } }
public async Task <IHttpActionResult> Update(int chiTietDoGiatID, [FromBody] ChiTietDoGiat chiTietDoGiat) { if (chiTietDoGiat.ChiTietDoGiatID != chiTietDoGiatID) { return(BadRequest("Id mismatch")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (var db = new ApplicationDbContext()) { ChiTietDoGiat chiTietDoGiatCu = await db.ChiTietDoGiat.SingleOrDefaultAsync(x => x.ChiTietDoGiatID == chiTietDoGiatID); try { using (var transaction = db.Database.BeginTransaction()) { KhachDatHang kdh = await db.KhachDatHang.SingleOrDefaultAsync(x => x.KhachDatHangID == chiTietDoGiat.KhachDatHangID); NguoiDung nguoiDung = await db.NguoiDung.SingleOrDefaultAsync(x => x.NguoiDungID == kdh.NguoiDungID); DoGiat doGiat = await db.DoGiat.SingleOrDefaultAsync(x => x.DoGiatID == chiTietDoGiat.DoGiatID); HinhThucGiat hinhThucGiat = await db.HinhThucGiat.SingleOrDefaultAsync(x => x.HinhThucGiatID == chiTietDoGiat.HinhThucGiatID); var giaBan = await db.DonGia.FirstOrDefaultAsync(x => x.DoGiatID == doGiat.DoGiatID && x.HinhThucGiatID == hinhThucGiat.HinhThucGiatID); int?donGia = giaBan.DonGiaGiat; kdh.ThanhTien = (kdh.ThanhTien - donGia * chiTietDoGiatCu.SoLuong + donGia * chiTietDoGiat.SoLuong); nguoiDung.DiemThuong = nguoiDung.DiemThuong - (donGia * chiTietDoGiatCu.SoLuong) / 100 + (donGia * chiTietDoGiat.SoLuong) / 100; chiTietDoGiatCu.KhachDatHangID = chiTietDoGiat.KhachDatHangID; chiTietDoGiatCu.HinhThucGiatID = chiTietDoGiat.HinhThucGiatID; chiTietDoGiatCu.DoGiatID = chiTietDoGiat.DoGiatID; chiTietDoGiatCu.SoLuong = chiTietDoGiat.SoLuong; await db.SaveChangesAsync(); transaction.Commit(); } } catch (DbUpdateConcurrencyException ducEx) { bool exists = db.ChiTietDoGiat.Count(o => o.ChiTietDoGiatID == chiTietDoGiatID) > 0; if (!exists) { return(NotFound()); } else { throw ducEx; } } return(Ok(chiTietDoGiat)); } }
public async Task <IHttpActionResult> Insert([FromBody] KhachDatHang khachDatHang) { if (khachDatHang.KhachDatHangID != 0) { return(BadRequest("Invalid KhachDatHangID")); } using (var db = new ApplicationDbContext()) { khachDatHang.ThanhTien = 0; khachDatHang.TinhTrangThanhToan = 2; khachDatHang.TinhTrangXuLy = 1; db.KhachDatHang.Add(khachDatHang); await db.SaveChangesAsync(); } return(Ok(khachDatHang)); }
public async Task <IHttpActionResult> Insert([FromBody] ChiTietDoGiat chiTietDoGiat) { if (chiTietDoGiat.ChiTietDoGiatID != 0) { return(BadRequest("Invalid ChiTietDoGiatID")); } using (var db = new ApplicationDbContext()) { using (var transaction = db.Database.BeginTransaction()) { KhachDatHang kdh = await db.KhachDatHang.SingleOrDefaultAsync(x => x.KhachDatHangID == chiTietDoGiat.KhachDatHangID); NguoiDung nguoiDung = await db.NguoiDung.SingleOrDefaultAsync(x => x.NguoiDungID == kdh.NguoiDungID); var giaBan = await db.DonGia.FirstOrDefaultAsync(x => x.DoGiatID == chiTietDoGiat.DoGiatID && x.HinhThucGiatID == chiTietDoGiat.HinhThucGiatID); if (giaBan != null) { int?donGia = giaBan.DonGiaGiat; kdh.ThanhTien += donGia * chiTietDoGiat.SoLuong; nguoiDung.DiemThuong = kdh.ThanhTien / 100; } var chiTietDoGiatDaCo = await db.ChiTietDoGiat.FirstOrDefaultAsync(x => x.KhachDatHangID == chiTietDoGiat.KhachDatHangID && x.DoGiatID == chiTietDoGiat.DoGiatID && x.HinhThucGiatID == chiTietDoGiat.HinhThucGiatID); if (chiTietDoGiatDaCo != null) { chiTietDoGiatDaCo.SoLuong += chiTietDoGiat.SoLuong; } else { db.ChiTietDoGiat.Add(chiTietDoGiat); } await db.SaveChangesAsync(); transaction.Commit(); } } return(Ok(chiTietDoGiat)); }
public async Task <IHttpActionResult> Delete(int chiTietDoGiatID) { using (var db = new ApplicationDbContext()) { using (var transaction = db.Database.BeginTransaction()) { var chiTietDoGiat = await db.ChiTietDoGiat.SingleOrDefaultAsync(o => o.ChiTietDoGiatID == chiTietDoGiatID); if (chiTietDoGiat == null) { return(NotFound()); } KhachDatHang kdh = await db.KhachDatHang.SingleOrDefaultAsync(x => x.KhachDatHangID == chiTietDoGiat.KhachDatHangID); NguoiDung nguoiDung = await db.NguoiDung.SingleOrDefaultAsync(x => x.NguoiDungID == kdh.NguoiDungID); DoGiat doGiat = await db.DoGiat.SingleOrDefaultAsync(x => x.DoGiatID == chiTietDoGiat.DoGiatID); HinhThucGiat hinhThucGiat = await db.HinhThucGiat.SingleOrDefaultAsync(x => x.HinhThucGiatID == chiTietDoGiat.HinhThucGiatID); var giaBan = await db.DonGia.SingleOrDefaultAsync(x => x.DoGiatID == doGiat.DoGiatID && x.HinhThucGiatID == hinhThucGiat.HinhThucGiatID); int?donGia = giaBan.DonGiaGiat; kdh.ThanhTien -= donGia * chiTietDoGiat.SoLuong; nguoiDung.DiemThuong -= (donGia * chiTietDoGiat.SoLuong) / 100; db.Entry(chiTietDoGiat).State = EntityState.Deleted; await db.SaveChangesAsync(); transaction.Commit(); return(Ok()); } } }