public async Task <ActionResult <CtphieuMuon> > PutCtphieuMuon(int id, CtphieuMuon ctphieuMuon)
        {
            if (id != ctphieuMuon.Id)
            {
                return(BadRequest());
            }

            _context.Entry(ctphieuMuon).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CtphieuMuonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            var new_ctphieuMuon = await _context.CtphieuMuon
                                  .Include(c => c.BookNavigation)
                                  .Include(c => c.PhieuMuonNavigation)
                                  .FirstOrDefaultAsync(m => m.Id == ctphieuMuon.Id);

            return(new_ctphieuMuon);
        }
        public async Task <IActionResult> muonsach()
        {
            var docgia    = HttpContext.Session.GetObject <DocGia>(CommonConstants.User_Session);
            var ss_lsSach = HttpContext.Session.GetObject <List <Models.SessionSach> >("dssach");
            var tongsach  = 0;

            ss_lsSach.ForEach(lssach => tongsach += lssach.soluong);
            if (docgia != null && ss_lsSach != null)
            {
                PhieuMuon pm = new PhieuMuon();
                pm.NgayMuon     = DateTime.Now;
                pm.MaDg         = docgia.Id;
                pm.TrangThai    = 1;
                pm.TongSachMuon = tongsach;
                pm.MaNv         = 1;
                pm.HanTra       = DateTime.Now.AddDays(30);
                if (pm.NgayMuon < pm.HanTra)
                {
                    var respond = await _apiService.PostAsJsonAsync("api/phieuMuon", pm).Result.Content.ReadAsAsync <PhieuMuon>();

                    for (int i = 0; i < ss_lsSach.Count; i++)
                    {
                        CtphieuMuon ctpm = new CtphieuMuon();
                        ctpm.PhieuMuon     = respond.Id;
                        ctpm.SoLuong       = ss_lsSach[i].soluong;
                        ctpm.Book          = ss_lsSach[i].sach.Id;
                        ctpm.TinhTrangSach = "1";

                        var sach = await _apiService.GetAsync($"api/sach/{ctpm.Book}").Result.Content.ReadAsAsync <Sach>();

                        if (sach.SoLuong >= ctpm.SoLuong)
                        {
                            sach.SoLuong -= ctpm.SoLuong;

                            CtphieuMuon new_ctPhieuMuon = await _apiService.PostAsJsonAsync("api/ctPhieuMuon", ctpm).Result.Content.ReadAsAsync <CtphieuMuon>();

                            HttpResponseMessage respond_sach = await _apiService.PutAsJsonAsync($"api/sach/{sach.Id}", sach);

                            respond_sach.EnsureSuccessStatusCode();
                            TempData["notice"] = "success";
                        }
                        else
                        {
                            TempData["notice"] = "fail";
                        }
                    }
                    ss_lsSach = new List <SessionSach>();
                    HttpContext.Session.SetObject("dssach", ss_lsSach);
                    return(Redirect("~/Home/Index"));
                }
            }
            else
            {
                TempData["notice"] = "fail";
                return(Redirect("~/checkout/checkout"));
            }

            return(Redirect("~/Home/Index"));
        }
Exemplo n.º 3
0
        // GET: CtphieuMuon/Create
        public async Task <IActionResult> Create(int PhieuMuonId)
        {
            CtphieuMuon chitietphieumuon = new CtphieuMuon();

            chitietphieumuon.PhieuMuon = PhieuMuonId;
            var list_sach = await _apiService.GetAsync("api/sach").Result.Content.ReadAsAsync <List <Sach> >();

            ViewData["Book"] = new SelectList(list_sach, "Id", "TenSach");
            return(View(chitietphieumuon));
        }
        public async Task <ActionResult <CtphieuMuon> > PostCtphieuMuon(CtphieuMuon ctphieuMuon)
        {
            _context.CtphieuMuon.Add(ctphieuMuon);
            await _context.SaveChangesAsync();

            var new_ctphieuMuon = await _context.CtphieuMuon
                                  .Include(c => c.BookNavigation)
                                  .Include(c => c.PhieuMuonNavigation)
                                  .FirstOrDefaultAsync(m => m.Id == ctphieuMuon.Id);

            return(new_ctphieuMuon);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,PhieuMuon,Book,SoLuong,NgayMuon,NgayTra,TinhTrangSach")] CtphieuMuon ctphieuMuon)
        {
            var ctPhieMuon_cu = await _apiService.GetAsync($"api/ctPhieuMuon/Detail/{id}").Result.Content.ReadAsAsync <CtphieuMuon>();

            int slpm_cu = ctPhieMuon_cu.SoLuong;

            ViewBag.SoLuongSPCu = slpm_cu;
            if (id != ctphieuMuon.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                Sach sach = await _apiService.GetAsync($"api/sach/{ctphieuMuon.Book}").Result.Content.ReadAsAsync <Sach>();

                int slSachThem = ctphieuMuon.SoLuong - slpm_cu;
                if (sach.SoLuong > slSachThem)
                {
                    CtphieuMuon entity = await _apiService.PutAsJsonAsync($"api/ctPhieuMuon/{id}", ctphieuMuon).Result.Content.ReadAsAsync <CtphieuMuon>();

                    sach.SoLuong = sach.SoLuong - slSachThem;
                    PhieuMuon phieuMuon = await _apiService.GetAsync($"api/phieuMuon/{ctphieuMuon.PhieuMuon}").Result.Content.ReadAsAsync <PhieuMuon>();

                    phieuMuon.TongSachMuon   = phieuMuon.TongSachMuon + slSachThem;
                    phieuMuon.MaDgNavigation = null;
                    phieuMuon.MaNvNavigation = null;
                    HttpResponseMessage respond_sach = await _apiService.PutAsJsonAsync($"api/sach/{sach.Id}", sach);

                    respond_sach.EnsureSuccessStatusCode();
                    HttpResponseMessage respond_phieuMuon = await _apiService.PutAsJsonAsync($"api/phieuMuon/{phieuMuon.Id}", phieuMuon);

                    respond_phieuMuon.EnsureSuccessStatusCode();

                    @TempData["notice"] = "Successfully edit";
                    @TempData["masp"]   = entity.Book;
                    @TempData["tensp"]  = entity.BookNavigation.TenSach;
                    return(RedirectToAction("Index", new { phieuMuonId = ctphieuMuon.PhieuMuon }));
                }
                @TempData["notice"] = "Error edit";
                @TempData["tesp"]   = ctPhieMuon_cu.BookNavigation.TenSach;
                @TempData["masp"]   = ctPhieMuon_cu.Book;
            }
            var list_sach = await _apiService.GetAsync("api/sach").Result.Content.ReadAsAsync <List <Sach> >();

            ViewData["Book"] = new SelectList(list_sach, "Id", "Id", ctphieuMuon.Book);
            var list_phieumuon = await _apiService.GetAsync("api/phieuMuon").Result.Content.ReadAsAsync <List <PhieuMuon> >();

            ViewData["PhieuMuon"] = new SelectList(list_phieumuon, "Id", "Id", ctphieuMuon.PhieuMuon);
            return(View(ctphieuMuon));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("Id,PhieuMuon,Book,SoLuong,NgayMuon,NgayTra,TinhTrangSach")] CtphieuMuon ctphieuMuon)
        {
            if (ModelState.IsValid)
            {
                var phieuMuon = await _apiService.GetAsync($"api/phieuMuon/{ctphieuMuon.PhieuMuon}").Result.Content.ReadAsAsync <PhieuMuon>();

                var sach = await _apiService.GetAsync($"api/sach/{ctphieuMuon.Book}").Result.Content.ReadAsAsync <Sach>();

                if (sach.SoLuong >= ctphieuMuon.SoLuong)
                {
                    phieuMuon.TongSachMuon  += ctphieuMuon.SoLuong;
                    phieuMuon.MaDgNavigation = null;
                    phieuMuon.MaNvNavigation = null;
                    sach.SoLuong            -= ctphieuMuon.SoLuong;

                    CtphieuMuon new_ctPhieuMuon = await _apiService.PostAsJsonAsync("api/ctPhieuMuon", ctphieuMuon).Result.Content.ReadAsAsync <CtphieuMuon>();

                    HttpResponseMessage respond_sach = await _apiService.PutAsJsonAsync($"api/sach/{sach.Id}", sach);

                    respond_sach.EnsureSuccessStatusCode();
                    HttpResponseMessage respond_phieuMuon = await _apiService.PutAsJsonAsync($"api/phieuMuon/{phieuMuon.Id}", phieuMuon);

                    respond_phieuMuon.EnsureSuccessStatusCode();

                    @TempData["notice"] = "Successfully create";
                    @TempData["tensp"]  = new_ctPhieuMuon.BookNavigation.TenSach;
                    @TempData["masp"]   = new_ctPhieuMuon.Book;
                    return(RedirectToAction("Index", new { phieuMuonId = ctphieuMuon.PhieuMuon }));
                }
                @TempData["notice"] = "Error create";
                @TempData["tensp"]  = sach.TenSach;
                @TempData["masp"]   = sach.Id;
            }
            var list_sach = await _apiService.GetAsync("api/sach").Result.Content.ReadAsAsync <List <Sach> >();

            ViewData["Book"] = new SelectList(list_sach, "Id", "TenSach", ctphieuMuon.Book);
            return(View(ctphieuMuon));
        }