public IActionResult Create(TaskViewModel model) {
     if (ModelState.IsValid) {
         _dataAccess.AddTask(model.ToModel(_dataAccess));
         return RedirectToAction("Index");
     }
     return View(model);
 }
Esempio n. 2
0
 public IActionResult Create(int activityId)
 {
     var activity = _dataAccess.GetActivity(activityId);
     if (activity == null || !User.IsTenantAdmin(activity.TenantId))
     {
         return HttpUnauthorized();
     }
     var viewModel = new TaskViewModel()
     {
         IsNew = true,
         ActivityId = activity.Id,
         ActivityName = activity.Name,
         CampaignId = activity.CampaignId,
         CampaignName = activity.Campaign.Name,
         TenantId = activity.TenantId,
         TenantName = activity.Tenant.Name
     };
     return View("Edit", viewModel);
 }
        public async Task PostSendsAddTaskCommandAsyncWithCorrectData()
        {
            var model = new TaskViewModel { EventId = 1, Id = 1 };
            var allReadyTask = new AllReadyTask();

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event());
            mediator.SetupSequence(x => x.Send(It.IsAny<TaskByTaskIdQuery>()))
                .Returns(allReadyTask)
                .Returns(null);

            var determineIfATaskIsEditable = new Mock<IDetermineIfATaskIsEditable>();
            determineIfATaskIsEditable.Setup(x => x.For(It.IsAny<ClaimsPrincipal>(), It.IsAny<AllReadyTask>())).Returns(true);

            var sut = new TaskApiController(mediator.Object, determineIfATaskIsEditable.Object);
            await sut.Post(model);

            mediator.Verify(x => x.SendAsync(It.Is<AddTaskCommandAsync>(y => y.AllReadyTask == allReadyTask)), Times.Once);
        }
Esempio n. 4
0
        public IActionResult Create(int activityId, TaskViewModel model)
        {
            if (model.EndDateTime < model.StartDateTime)
            {
                ModelState.AddModelError("EndDateTime", "Ending time cannot be earlier than the starting time");
            }

            if (ModelState.IsValid)
            {
                var activity = _dataAccess.GetActivity(model.ActivityId);
                if (activity == null || !User.IsTenantAdmin(activity.TenantId))
                {
                    return HttpUnauthorized();
                }
                _dataAccess.AddTaskAsync(model.ToModel(_dataAccess));
                return RedirectToAction("Details", "Activity", new { id = activityId });
            }
            model.IsNew = true;
            return View("Edit", model);
        }
Esempio n. 5
0
 public IActionResult Edit(int id)
 {
     var dbTask = _dataAccess.GetTask(id);
     if (dbTask == null || dbTask.Activity == null)
     {
         return HttpNotFound();
     }            
     if (!User.IsTenantAdmin(dbTask.Activity.TenantId))
     {
         return HttpUnauthorized();
     }
     var model = new TaskViewModel(dbTask);
     return View(model);
 }
Esempio n. 6
0
        public IActionResult Details(int activityId, int id)
        {

            var dbTask = _dataAccess.GetTask(id);
            if (dbTask == null)
            {
                return new HttpNotFoundResult();
            }

            var model = new TaskViewModel(dbTask);

            return View(model);
        }
 public IActionResult Edit(int id)
 {
     var dbTask = _dataAccess.GetTask(id);
     var model = new TaskViewModel {
         Id = dbTask.Id,
         Name = dbTask.Name,
         Description = dbTask.Description,
         ActivityId = dbTask.Activity.Id,
         ActivityName = dbTask.Activity.Name,
         StartDateTime = dbTask.StartDateTimeUtc,
         EndDateTime = dbTask.EndDateTimeUtc
     };
     return View(model);
 }
        public IActionResult Delete(int id) {

            var dbTask = _dataAccess.GetTask(id);
            if (dbTask == null) {
                return new HttpStatusCodeResult(404);
            }

            var model = new TaskViewModel {
                Id = dbTask.Id,
                Name = dbTask.Name,
                StartDateTime = dbTask.StartDateTimeUtc,
                EndDateTime = dbTask.EndDateTimeUtc
            };

            return View(model);
        }
        public async Task<IActionResult> Edit(TaskViewModel model)
        {
            if (ModelState.IsValid)
            {
                await _dataAccess.UpdateTask(model.ToModel(_dataAccess));
                if (ViewBag.ActivityId != null)
                {
                    return RedirectToAction("Index", new { activityId = ViewBag.ActivityId });
                }
                else
                {
                    return RedirectToAction("Index");
                }                
            }

            return View(model);
        }
Esempio n. 10
0
 private bool IfTaskExists(TaskViewModel task)
 {
     return GetTaskBy(task.Id) != null;
 }
        public async Task PostSendsTaskByTaskIdQueryWithCorrectTaskId()
        {
            var model = new TaskViewModel { EventId = 1, Id = 1 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event());
            mediator.Setup(x => x.Send(It.IsAny<TaskByTaskIdQuery>())).Returns(new AllReadyTask());

            var provider = new Mock<IDetermineIfATaskIsEditable>();
            provider.Setup(x => x.For(It.IsAny<ClaimsPrincipal>(), It.IsAny<AllReadyTask>())).Returns(true);

            var sut = new TaskApiController(mediator.Object, provider.Object);
            await sut.Post(model);

            mediator.Verify(x => x.Send(It.Is<TaskByTaskIdQuery>(y => y.TaskId == model.Id)));
        }
        public async Task PutReturnsHttpStatusCodeResultOf204()
        {
            var allReadyTask = new AllReadyTask();
            var model = new TaskViewModel { Name = "name", Description = "description", StartDateTime = DateTime.UtcNow, EndDateTime = DateTime.UtcNow };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<TaskByTaskIdQuery>())).Returns(allReadyTask);

            var determineIfATaskIsEditable = new Mock<IDetermineIfATaskIsEditable>();
            determineIfATaskIsEditable.Setup(x => x.For(It.IsAny<ClaimsPrincipal>(), It.IsAny<AllReadyTask>())).Returns(true);

            var sut = new TaskApiController(mediator.Object, determineIfATaskIsEditable.Object);
            var result = await sut.Put(It.IsAny<int>(), model) as HttpStatusCodeResult;

            Assert.IsType<HttpStatusCodeResult>(result);
            Assert.Equal(result.StatusCode, 204);
        }
        public async Task PutSendsUpdateTaskCommandAsyncWithCorrectAllReadyTask()
        {
            var allReadyTask = new AllReadyTask();
            var model = new TaskViewModel { Name = "name", Description = "description", StartDateTime = DateTime.UtcNow, EndDateTime = DateTime.UtcNow };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<TaskByTaskIdQuery>())).Returns(allReadyTask);

            var determineIfATaskIsEditable = new Mock<IDetermineIfATaskIsEditable>();
            determineIfATaskIsEditable.Setup(x => x.For(It.IsAny<ClaimsPrincipal>(), It.IsAny<AllReadyTask>())).Returns(true);

            var sut = new TaskApiController(mediator.Object, determineIfATaskIsEditable.Object);
            await sut.Put(It.IsAny<int>(), model);

            mediator.Verify(x => x.SendAsync(It.Is<UpdateTaskCommandAsync>(y => y.AllReadyTask == allReadyTask)), Times.Once);
        }
        public async Task PostReturnsHttpStatusCodeResultOf201()
        {
            var model = new TaskViewModel { EventId = 1, Id = 0 };

            var mediator = new Mock<IMediator>();
            mediator.Setup(x => x.Send(It.IsAny<EventByIdQuery>())).Returns(new Event());

            var provider = new Mock<IDetermineIfATaskIsEditable>();
            provider.Setup(x => x.For(It.IsAny<ClaimsPrincipal>(), It.IsAny<AllReadyTask>())).Returns(true);

            var sut = new TaskApiController(mediator.Object, provider.Object);
            var result = await sut.Post(model) as HttpStatusCodeResult;

            Assert.IsType<HttpStatusCodeResult>(result);
            Assert.Equal(result.StatusCode, 201);
        }
Esempio n. 15
0
        public IActionResult Details(int activityId, int id)
        {

            var dbTask = _dataAccess.GetTask(id);
            if (dbTask == null)
            {
                return new HttpStatusCodeResult(404);
            }

            var model = new TaskViewModel
            {
                Id = dbTask.Id,
                ActivityId = activityId,
                Description = dbTask.Description,
                Name = dbTask.Name,
                StartDateTime = dbTask.StartDateTimeUtc,
                EndDateTime = dbTask.EndDateTimeUtc
            };

            return View(model);
        }
Esempio n. 16
0
        public static AllReadyTask ToModel(this TaskViewModel taskViewModel, IAllReadyDataAccess dataAccess)
        {
            var activity = dataAccess.GetActivity(taskViewModel.ActivityId);

            if (activity == null)
            {
                return(null);
            }

            var          newTask = true;
            AllReadyTask dbtask;

            if (taskViewModel.Id == 0)
            {
                dbtask = new AllReadyTask();
            }
            else
            {
                dbtask  = dataAccess.GetTask(taskViewModel.Id);
                newTask = false;
            }

            dbtask.Id                    = taskViewModel.Id;
            dbtask.Description           = taskViewModel.Description;
            dbtask.Activity              = activity;
            dbtask.EndDateTime           = taskViewModel.EndDateTime.HasValue ? taskViewModel.EndDateTime.Value.UtcDateTime : new DateTime?();
            dbtask.StartDateTime         = taskViewModel.EndDateTime.HasValue ? taskViewModel.StartDateTime.Value.UtcDateTime : new DateTime?();
            dbtask.Name                  = taskViewModel.Name;
            dbtask.RequiredSkills        = dbtask.RequiredSkills ?? new List <TaskSkill>();
            taskViewModel.RequiredSkills = taskViewModel.RequiredSkills ?? new List <int>();
            ////Remove old skills
            //dbtask.RequiredSkills.RemoveAll(ts => !taskViewModel.RequiredSkills.Any(s => ts.SkillId == s));
            ////Add new skills
            //dbtask.RequiredSkills.AddRange(taskViewModel.RequiredSkills
            //    .Where(rs => !dbtask.RequiredSkills.Any(ts => ts.SkillId == rs))
            //    .Select(rs => new TaskSkill() { SkillId = rs, TaskId = taskViewModel.Id }));

            // Workaround:  POST is bringing in empty AssignedVolunteers.  Clean this up. Discussing w/ Kiran Challa.
            // Workaround: the if statement is superflous, and should go away once we have the proper fix referenced above.
            if (taskViewModel.AssignedVolunteers != null)
            {
                var bogusAssignedVolunteers = (from assignedVolunteer in taskViewModel.AssignedVolunteers
                                               where string.IsNullOrEmpty(assignedVolunteer.UserId)
                                               select assignedVolunteer).ToList();
                foreach (var bogus in bogusAssignedVolunteers)
                {
                    taskViewModel.AssignedVolunteers.Remove(bogus);
                }
            }
            // end workaround

            if (taskViewModel.AssignedVolunteers != null && taskViewModel.AssignedVolunteers.Count > 0)
            {
                var taskUsersList = taskViewModel.AssignedVolunteers.Select(tvm => new TaskSignup
                {
                    Task = dbtask,
                    User = dataAccess.GetUser(tvm.UserId)
                }).ToList();

                // We may be updating an existing task
                if (newTask || dbtask.AssignedVolunteers.Count == 0)
                {
                    dbtask.AssignedVolunteers = taskUsersList;
                }
                else
                {
                    // Can probably rewrite this more efficiently.
                    foreach (TaskSignup taskUsers in taskUsersList)
                    {
                        if (!(from entry in dbtask.AssignedVolunteers
                              where entry.User.Id == taskUsers.User.Id
                              select entry).Any())
                        {
                            dbtask.AssignedVolunteers.Add(taskUsers);
                        }
                    }
                }
            }

            return(dbtask);
        }