Пример #1
0
        public void GetAll_with_exist_events()
        {
            var event0 = new Event() { Id = 0, Name = "name0", BeginDate = DateTime.Now.AddDays(1), EndDate = DateTime.Now.AddDays(2) };
            var event1 = new Event() { Id = 1, Name = "name1", BeginDate = DateTime.Now.AddDays(2), EndDate = DateTime.Now.AddDays(3) };
            var event2 = new Event() { Id = 2, Name = "name2", BeginDate = new DateTime(2012, 10, 1), EndDate = new DateTime(2012, 10, 2) };
            var event3 = new Event() { Id = 3, Name = "name3", BeginDate = new DateTime(2001, 10, 10), EndDate = new DateTime(2002, 1, 1) };

            var target = InitializeController(new[] { event0, event1, event2, event3 });

            dynamic json = target.GetAll().Data;
            var futureEvents = ((IEnumerable<dynamic>)json.futureEvents).ToArray();
            var pastEvents = ((IEnumerable<dynamic>)json.pastEvents).ToArray();

            Assert.AreEqual("valid", json.status, "the status property contains wrong value");

            Assert.AreEqual(0, futureEvents[0].id, "futureEvents[0].id contains wrong value");
            Assert.AreEqual(1, futureEvents[1].id, "futureEvents[1].id contains wrong value");
            Assert.AreEqual("name0", futureEvents[0].name, "futureEvents[0].name contains wrong value");
            Assert.AreEqual("name1", futureEvents[1].name, "futureEvents[1].name contains wrong value");
            Assert.AreEqual(event0.BeginDate.ToString(CultureInfo.InvariantCulture), futureEvents[0].beginDate, "futureEvents[0].beginDate contains wrong value");
            Assert.AreEqual(event1.BeginDate.ToString(CultureInfo.InvariantCulture), futureEvents[1].beginDate, "futureEvents[1].beginDate contains wrong value");
            Assert.AreEqual(event0.EndDate.ToString(CultureInfo.InvariantCulture), futureEvents[0].endDate,"futureEvents[0].endDate contains wrong value");
            Assert.AreEqual(event1.EndDate.ToString(CultureInfo.InvariantCulture), futureEvents[1].endDate, "futureEvents[1].endDate contains wrong value");

            Assert.AreEqual(3, pastEvents[0].id, "pastEvents[0].id contains wrong value");
            Assert.AreEqual(2, pastEvents[1].id, "pastEvents[1].id contains wrong value");
            Assert.AreEqual("name3", pastEvents[0].name, "pastEvents[0].name contains wrong value");
            Assert.AreEqual("name2", pastEvents[1].name, "pastEvents[1].name contains wrong value");
            Assert.AreEqual(event3.BeginDate.ToString(CultureInfo.InvariantCulture), pastEvents[0].beginDate, "pastEvents[0].beginDate contains wrong value");
            Assert.AreEqual(event2.BeginDate.ToString(CultureInfo.InvariantCulture), pastEvents[1].beginDate, "pastEvents[1].beginDate contains wrong value");
            Assert.AreEqual(event3.EndDate.ToString(CultureInfo.InvariantCulture), pastEvents[0].endDate, "pastEvents[0].endDate contains wrong value");
            Assert.AreEqual(event2.EndDate.ToString(CultureInfo.InvariantCulture), pastEvents[1].endDate, "pastEvents[1].endDate contains wrong value");
        }
Пример #2
0
 public ActionResult Create(CreateEventViewModel model)
 {
     if (ModelState.IsValid)
     {
         var newEvent = new Event { Id = -1, Name = model.Name, BeginDate = model.BeginTime, EndDate = model.EndTime };
         if (IsIntersected(newEvent))
         {
             return RedirectToAction("Intersection");
         }
         _db.Add(newEvent);
         return RedirectToAction("GetAll");
     }
     return Json(new { status = "error", errors = GetErrors(ModelState) }, JsonRequestBehavior.DenyGet);
 }
Пример #3
0
        public void GetExistingEvent()
        {
            var entry = new Event() { Id = 0, BeginDate = DateTime.Now, EndDate = DateTime.Now.AddDays(1), Name = "n0" };
            var target = InitializeController(new List<Event>() { entry });

            dynamic json = target.Update(0).Data;

            Assert.AreEqual(json.status, "valid", "The isValid property has a wrong value");

            var jsonEntry = json.entry;
            Assert.AreEqual(entry.Id, jsonEntry.id, "The entry property has a wrong value");
            Assert.AreEqual(entry.BeginDate.ToString(CultureInfo.InvariantCulture), jsonEntry.beginTime, "The beginTime property has a wrong value");
            Assert.AreEqual(entry.EndDate.ToString(CultureInfo.InvariantCulture), jsonEntry.endTime, "The endTime property has a wrong value");
            Assert.AreEqual(entry.Name, jsonEntry.name, "The name property has a wrong value");
        }
Пример #4
0
        public ActionResult Update(UpdateEventViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newEvent = new Event
                {
                    Id = model.Id,
                    Name = model.Name,
                    BeginDate = model.BeginTime,
                    EndDate = model.EndTime
                };

                if (IsIntersected(newEvent))
                {
                    return RedirectToAction("Intersection");
                }

                try
                {
                    _db.Update(newEvent);
                    return RedirectToAction("GetAll");
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", "Oops something went wrong");
                }
            }
            return Json(new { status = "error", errors = GetErrors(ModelState) }, JsonRequestBehavior.DenyGet);
        }
Пример #5
0
        private bool IsIntersected(Event newEvent)
        {
            var events = _db.GetAll(old => newEvent.EndDate > old.BeginDate && old.EndDate > newEvent.BeginDate).Where(n => n.Id != newEvent.Id);
            var result = events.Count() != 0;

            if (result)
            {
                Session.Add("Intersection", new IntersectedEvents
                {
                    IsEmpty = false,
                    NewEvent = newEvent,
                    OldEvents = events
                });
            }
            return result;

        }
Пример #6
0
 public void Update(Event entry)
 {
     _db.Entry(entry).State = EntityState.Modified;
     _db.SaveChanges();
 }
Пример #7
0
 public void Delete(int id)
 {
     var entry = new Event { Id = id };
     _db.Entry(entry).State = EntityState.Deleted;
     _db.SaveChanges();
 }
Пример #8
0
 public void Add(Event entry)
 {
     _db.Entry(entry).State = EntityState.Added;
     _db.SaveChanges();
 }