예제 #1
0
 public ActionResult Create(int projectId, int templateId)
 {
     EditBidPackageViewModel viewModel = new EditBidPackageViewModel();
     viewModel.ProjectId = projectId;
     viewModel.TemplateId = templateId;
     viewModel.BidDateTime = DateTime.Now;
     viewModel.UseProjectBidDate = true;
     viewModel.UseProjectWalkThru = true;
     return View("Create", viewModel);
 }
예제 #2
0
        public ActionResult Create(EditBidPackageViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                int userId = _security.GetUserId(User.Identity.Name);
                int companyId = _service.GetUser(userId).CompanyId;

                try
                {
                    BidPackage toCreate = new BidPackage
                            {
                                BidDateTime = viewModel.BidDateTime,
                                CreatedById = companyId,
                                Description = viewModel.Description,
                                IsMaster = false,
                                ProjectId = viewModel.ProjectId,
                                TemplateBidPackageId = viewModel.TemplateId,
                                Notes = viewModel.Notes
                            };

                    toCreate.Scopes = new List<BidPackageXScope>();

                    for (int i = 0; i < viewModel.SelectedScope.Count(); i++)
                    {
                        toCreate.Scopes.Add(new BidPackageXScope { BidPackage = toCreate, ScopeId = viewModel.SelectedScope.ElementAt(i) });
                    }

                    if (_service.Create(toCreate))
                    {
                        return RedirectToRoute("Project_default", new { controller = "BidPackage", action = "Details", id = toCreate.Id });
                    }
                    else
                    {
                        Util.MapValidationErrors(_service.ValidationDic, this.ModelState);
                        return View("Create", viewModel);
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Exception", ex.Message);
                    return View("Create", viewModel);
                }

            }
            else
            {
                return View("Create", viewModel);
            }
        }
        public void PostCreateBidPackageExceptionReturnsModelStateErrors()
        {
            // arrange
            Mock<IBidPackageServiceLayer> service = new Mock<IBidPackageServiceLayer>();
            service.Setup(s => s.Create(It.IsAny<BidPackage>())).Throws(new Exception("my spoon is too big"));
            service.SetupGet(s => s.ValidationDic).Returns(new Dictionary<string, string> { { "Description", "You already have a bid package with this description" } });

            service.Setup(s => s.GetUser(It.IsAny<int>())).Returns(new UserProfile { CompanyId = 1, UserId = 1 });
            Mock<IWebSecurityWrapper> security = new Mock<IWebSecurityWrapper>();
            security.Setup(s => s.GetUserId("*****@*****.**")).Returns(1);

            Mock<INotificationSender> notice = new Mock<INotificationSender>();

            BidPackageController controller = new BidPackageController(service.Object, security.Object, notice.Object);

            Mock<IPrincipal> principal = new Mock<IPrincipal>();
            principal.Setup(p => p.Identity.Name).Returns("*****@*****.**");

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.SetupGet(c => c.HttpContext.User.Identity.Name).Returns("*****@*****.**");
            context.SetupGet(c => c.HttpContext.Request.IsAuthenticated).Returns(true);

            controller.ControllerContext = context.Object;

            EditBidPackageViewModel viewModel = new EditBidPackageViewModel
            {
                ProjectId = 1,
                TemplateId = 1,
                SelectedScope = new int[] { 1, 2, 3 },
                BidDateTime = new DateTime(2014, 2, 2, 17, 0, 0),
                Description = "booga booga"
            };

            // act
            var result = controller.Create(viewModel);

            // assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsTrue(((ViewResult)result).ViewData.ModelState.ContainsKey("Exception"));
            Assert.AreEqual("my spoon is too big", ((ViewResult)result).ViewData.ModelState["Exception"].Errors[0].ErrorMessage.ToString());
        }
        public void PostCreateValidBidPackageRedirectsToDetails()
        {
            // arrange
            Mock<IBidPackageServiceLayer> service = new Mock<IBidPackageServiceLayer>();
            service.Setup(s => s.Create(It.IsAny<BidPackage>())).Returns(true);
            service.Setup(s => s.GetUser(It.IsAny<int>())).Returns(new UserProfile { CompanyId = 1, UserId = 1 });
            Mock<IWebSecurityWrapper> security = new Mock<IWebSecurityWrapper>();
            security.Setup(s => s.GetUserId("*****@*****.**")).Returns(1);
            Mock<INotificationSender> notice = new Mock<INotificationSender>();

            BidPackageController controller = new BidPackageController(service.Object, security.Object, notice.Object);

            Mock<IPrincipal> principal = new Mock<IPrincipal>();
            principal.Setup(p => p.Identity.Name).Returns("*****@*****.**");

            Mock<ControllerContext> context = new Mock<ControllerContext>();
            context.SetupGet(c => c.HttpContext.User.Identity.Name).Returns("*****@*****.**");
            context.SetupGet(c => c.HttpContext.Request.IsAuthenticated).Returns(true);

            controller.ControllerContext = context.Object;

            EditBidPackageViewModel viewModel = new EditBidPackageViewModel
            {
                ProjectId = 1,
                TemplateId = 1,
                SelectedScope = new int[] { 1, 2, 3 },
                BidDateTime = new DateTime(2014, 2, 2, 17, 0, 0),
                Description = "booga booga"
            };

            // act
            var result = controller.Create(viewModel);

            // assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual("BidPackage", ((RedirectToRouteResult)result).RouteValues["controller"].ToString());
            Assert.AreEqual("Details", ((RedirectToRouteResult)result).RouteValues["action"].ToString());
        }
예제 #5
0
        public ActionResult Edit(EditBidPackageViewModel viewModel)
        {
            if (!viewModel.UseProjectBidDate && !viewModel.BidDateTime.HasValue)
                ModelState.AddModelError("BidDateTime", "Pleae provide a Bid Date and Time if not use the Project's Bid Date and Time");

            if (!viewModel.UseProjectWalkThru && !viewModel.WalkThruStatus.HasValue)
                ModelState.AddModelError("WalkThruStatus", "Please Select a Walk Through option if not using Project's Walk Through settings");

            if (!viewModel.UseProjectWalkThru &&
                viewModel.WalkThruStatus.HasValue &&
                viewModel.WalkThruStatus.Value == WalkThruStatus.WalkThruIncluded &&
                !viewModel.WalkThruDateTime.HasValue)
                ModelState.AddModelError("WalkThruDateTime", "Please set a Walk Through Date and Time");

            if (ModelState.IsValid)
            {
                int userId = _security.GetUserId(User.Identity.Name);
                int companyId = _service.GetUser(userId).CompanyId;

                try
                {
                    BidPackage toUpdate = _service.Get(viewModel.Id);

                    if (!viewModel.UseProjectBidDate && viewModel.BidDateTime.HasValue)
                        toUpdate.BidDateTime = viewModel.BidDateTime.Value;
                    else
                        toUpdate.BidDateTime = default(DateTime?);

                    if (!viewModel.UseProjectWalkThru && viewModel.WalkThruStatus.HasValue)
                        toUpdate.WalkThruStatus = viewModel.WalkThruStatus.Value;
                    else
                        toUpdate.WalkThruStatus = default(WalkThruStatus?);

                    if (!viewModel.UseProjectWalkThru &&
                        viewModel.WalkThruStatus.HasValue &&
                        viewModel.WalkThruStatus.Value == WalkThruStatus.WalkThruIncluded &&
                        viewModel.WalkThruDateTime.HasValue)
                        toUpdate.WalkThruDateTime = viewModel.WalkThruDateTime.Value;
                    else
                    {
                        toUpdate.WalkThruDateTime = default(DateTime?);
                    }

                    toUpdate.UseProjectBidDateTime = viewModel.UseProjectBidDate;
                    toUpdate.UseProjectWalkThruDateTime = viewModel.UseProjectWalkThru;
                    toUpdate.Description = viewModel.Description;
                    toUpdate.Notes = viewModel.Notes;

                    // get scopes that have been added
                    IEnumerable<int> toAdd = viewModel.SelectedScope.Where(s => !toUpdate.Scopes.Select(x => x.ScopeId).Contains(s));

                    // get scopes that have been removed
                    IEnumerable<BidPackageXScope> toRemove = toUpdate.Scopes.Where(s => !viewModel.SelectedScope.Contains(s.ScopeId));

                    // add new scopes
                    for (int i = 0; i < toAdd.Count(); i++)
                    {
                        toUpdate.Scopes.Add(new BidPackageXScope { BidPackage = toUpdate, ScopeId = toAdd.ElementAt(i) });
                    }

                    // remove scopes
                    for (int i = 0; i < toRemove.Count(); i++)
                    {
                        toUpdate.Scopes.Remove(toRemove.ElementAt(i));
                    }

                    // try to update db record
                    if (_service.Update(toUpdate))
                    {

                        // notify invited companies
                        int[] invitees = _notice.GetInvitationsNotDeclined(toUpdate.ProjectId, companyId).Select(i => i.SentToId).ToArray();

                        for (int i = 0; i < invitees.Length; i++)
                        {
                            _notice.SendNotification(invitees[i], RecipientType.company, NotificationType.ProjectChange, toUpdate.ProjectId, EntityType.Project);
                        }
                        return RedirectToRoute("Project_default", new { controller = "BidPackage", action = "Details", id = toUpdate.Id });
                    }
                    else
                    {
                        Util.MapValidationErrors(_service.ValidationDic, this.ModelState);
                        return View("Edit", viewModel);
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Exception", ex.Message);
                    return View("Edit", viewModel);
                }

            }
            else
            {
                return View("Edit", viewModel);
            }
        }
예제 #6
0
        public ActionResult Edit(int id)
        {
            var raw = _service.Get(id);
            EditBidPackageViewModel viewModel = new EditBidPackageViewModel
            {
                Description = raw.Description,
                Id = raw.Id,
                Notes = raw.Notes,
                ProjectId = raw.ProjectId,
                TemplateId = raw.TemplateBidPackageId,
                UseProjectWalkThru = raw.UseProjectWalkThruDateTime,
                UseProjectBidDate = raw.UseProjectBidDateTime,
                BidDateTime = raw.UseProjectBidDateTime ? default(DateTime?) : raw.BidDateTime.Value,
                WalkThruStatus = raw.UseProjectWalkThruDateTime ? default(WalkThruStatus?) : raw.WalkThruStatus.HasValue ? raw.WalkThruStatus.Value : default(WalkThruStatus?),
                WalkThruDateTime = raw.WalkThruStatus.HasValue && raw.WalkThruStatus.Value == WalkThruStatus.WalkThruIncluded ? raw.WalkThruDateTime.Value : default(DateTime?)
            };

            return View("Edit", viewModel);
        }