Example #1
0
        public IHttpActionResult GetMatDien(int id)
        {
            MatDien matDien = db.MatDiens.Find(id);

            if (matDien == null)
            {
                return(NotFound());
            }
            var tmpNhaTram = db.NhaTrams.SingleOrDefault(u => u.IDNhaTram == matDien.IDNhaTram);
            var tmpTram    = db.Trams.SingleOrDefault(u => u.IDTram == tmpNhaTram.IDTram);

            if (!isAccess(tmpTram.IDQuanLy))
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }
            return(Ok(matDien));
        }
Example #2
0
        public IHttpActionResult PostMatDien(MatDien matDien)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var tmpNhaTram = db.NhaTrams.SingleOrDefault(u => u.IDNhaTram == matDien.IDNhaTram);

            if (tmpNhaTram == null)
            {
                return(NotFound());
            }
            var tmpTram = db.Trams.SingleOrDefault(u => u.IDTram == tmpNhaTram.IDTram);

            if (tmpTram == null)
            {
                return(NotFound());
            }
            if (!isAccess(tmpTram.IDQuanLy))
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }

            DateTime UTCNow = DateTime.UtcNow.AddHours(7);

            if (matDien.NgayMatDien.Subtract(UTCNow).TotalDays > 0)
            {
                return(BadRequest());
            }
            else if (matDien.NgayMatDien.Subtract(UTCNow).TotalDays == 0)
            {
                if (matDien.GioMatDien.Subtract(matDien.ThoiGianMayNo).TotalMinutes > 0)
                {
                    return(BadRequest());
                }
                if (matDien.ThoiGianMayNo.Subtract(matDien.ThoiGianNgung).TotalMinutes > 0)
                {
                    return(BadRequest());
                }
                if (matDien.ThoiGianNgung.Subtract(UTCNow.TimeOfDay).TotalMinutes > 0)
                {
                    return(BadRequest());
                }
            }


            matDien.TongThoiGianChay = matDien.ThoiGianNgung.Subtract(matDien.ThoiGianMayNo);

            TimeSpan thoiGianTre = matDien.ThoiGianMayNo.Subtract(matDien.GioMatDien);

            TimeSpan thoiGianChamUngCuu;

            if (matDien.QuangDuongDiChuyen <= 20)
            {
                thoiGianChamUngCuu = DateTime.ParseExact("01:00", "HH:mm", CultureInfo.InvariantCulture).TimeOfDay;
            }
            else
            {
                thoiGianChamUngCuu = DateTime.ParseExact("02:00", "HH:mm", CultureInfo.InvariantCulture).TimeOfDay;
            }

            if (thoiGianTre.Subtract(thoiGianChamUngCuu).TotalMinutes < 0)
            {
                matDien.TienPhat = 0;
            }
            else
            {
                matDien.TienPhat = TIENPHAT;
            }
            db.MatDiens.Add(matDien);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = matDien.IDMatDien }, matDien));
        }