Exemplo n.º 1
0
        public async Task <IActionResult> PutLuotmuon(string mathe, string masach, Luotmuon luotmuon)
        {
            if (mathe != luotmuon.Mathe)
            {
                return(BadRequest());
            }
            if (masach != luotmuon.Masach)
            {
                return(BadRequest());
            }
            //

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!LuotmuonExists(mathe, masach))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Luotmuon> > PostLuotmuon(Luotmuon luotmuon)
        {
            //
            luotmuon.Ngaytra = DateTime.Now;
            var _user = await _context.User.FindAsync(luotmuon.Mathe);

            // _user.Sosachdamuon--;
            var _phieumuon = await _context.Phieumuon.FindAsync(luotmuon.Mathe, luotmuon.Masach);

            var _dattruoc = await _context.Dattruoc.FindAsync(luotmuon.Mathe, luotmuon.Masach);

            luotmuon.Ngaymuon   = _phieumuon.Ngaymuon;
            luotmuon.Ngayhethan = _phieumuon.Ngayhethan;
            //
            var _sach = await _context.Sach.FindAsync(_phieumuon.Masach);

            var _nhomsach = await _context.Nhomsach.FindAsync(_sach.Manhomsach);

            //tien phat = songayquahan*5000 hoac tienphat= giatien*3;
            if (luotmuon.Tinhtrangsachluctra == false)
            {
                //sach bi mat thi tru soluong di
                _sach.Tinhtrangsach = false;
                _nhomsach.Soluong--;
                _nhomsach.Soluongcon--;
                if (luotmuon.Ngaytra > luotmuon.Ngayhethan)
                {
                    TimeSpan _time        = luotmuon.Ngaytra - luotmuon.Ngayhethan;
                    int      songayquahan = _time.Days;
                    luotmuon.Tienphat = 5000 * songayquahan + _nhomsach.Giatien * 3;
                }
                else
                {
                    TimeSpan _time        = luotmuon.Ngaytra - luotmuon.Ngayhethan;
                    int      songayquahan = _time.Days;
                    luotmuon.Tienphat = _nhomsach.Giatien * 3;
                }
            }
            else
            {
                if (luotmuon.Ngaytra > luotmuon.Ngayhethan)
                {
                    TimeSpan _time        = luotmuon.Ngaytra - luotmuon.Ngayhethan;
                    int      songayquahan = _time.Days;
                    luotmuon.Tienphat = 5000 * songayquahan;
                }
            }

            _context.Luotmuon.Add(luotmuon);
            //xoa phieu muon va dattruoc khi luot muon duoc tao ra.
            _context.Phieumuon.Remove(_phieumuon);
            if (_dattruoc != null)
            {
                _context.Dattruoc.Remove(_dattruoc);
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (LuotmuonExists(luotmuon.Mathe, luotmuon.Masach))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            //return CreatedAtAction("GetLuotmuon", new { id = luotmuon.Mathe }, luotmuon);
            return(Ok());
        }