示例#1
0
        public void AddTagsAlreadyExistingAndNotExisting_ShouldReturnExpected()
        {
            List <Tag> tagsToAdd = new List <Tag>()
            {
                new Tag()
                {
                    Name = "lunch"
                },                          // existing
                new Tag()
                {
                    Name = "SQL"
                },                        // existing
                new Tag()
                {
                    Name = "sql"
                },                        // existing
                new Tag()
                {
                    Name = "newtag"
                },
                new Tag()
                {
                    Name = "UNIQUETag"
                },
                new Tag()
                {
                    Name = "uniquetag"
                },                              // duplicate
            };

            _utilityManager.AddNewTagsToDb(tagsToAdd);

            // Original count is 6
            var expectedCount = 8;

            var resultCount = _utilityManager.GetAllTags().Count();

            Assert.AreEqual(expectedCount, resultCount);
            Assert.AreEqual("uniquetag", _utilityManager.GetAllTags().SingleOrDefault(n => n.Name == "uniquetag").Name);
            Assert.AreNotEqual(1, _utilityManager.GetAllTags().Count(n => n.Name == "UNIQUETag"));
        }
        //[ValidateAntiForgeryToken]
        public ActionResult CreateSession(CreateSessionViewModel sessionVm)
        {
            if (ModelState.IsValid)
            {
                List <Participant> participantsToAddFromDb = new List <Participant>();
                if (sessionVm.AddedParticipants != null)
                {
                    string[]   participants   = sessionVm.AddedParticipants.Split(',');
                    List <int> participantsId = new List <int>();
                    foreach (var participant in participants)
                    {
                        participantsId.Add(int.Parse(participant));
                    }

                    //TODO: Make a proper join, this is inefficient
                    participantsToAddFromDb =
                        _personManager.GetAllParticipants().Where(n => participantsId.Contains(n.Id)).ToList();
                }


                // -> TAGS
                var tagsToAdd = sessionVm.GenerateSessionTags;

                _utilitiesManager.AddNewTagsToDb(tagsToAdd);
                // <- END TAGS


                int?locationId = _utilitiesManager.GetIdForLocationOrCreateIfNotExists(sessionVm.NameOfLocation);

                var result = new Session()
                {
                    Name        = sessionVm.Name,
                    ActivityId  = sessionVm.Activity.Id,
                    StartDate   = sessionVm.StartDate,
                    EndDate     = sessionVm.EndDate,
                    LocationId  = locationId,
                    HrPersonId  = sessionVm.HrPerson,
                    Description = sessionVm.Description,
                    IsOpenForExpressionOfInterest = sessionVm.IsOpenForExpressionOfInterest,
                    SessionParticipants           = null,
                    SessionTags = null
                };

                if (sessionVm.AddedParticipants != null)
                {
                    List <SessionParticipant> final = new List <SessionParticipant>();
                    foreach (var participant in participantsToAddFromDb)
                    {
                        final.Add(new SessionParticipant()
                        {
                            ParticipantId = participant.Id,
                            Session       = result,
                            Rating        = 0,
                        });
                        result.SessionParticipants = final;
                    }
                }

                // Save session in db
                if (_sessionManager.AddSession(result))
                {
                    // Now add tags to the created session!...
                    _sessionManager.AddSessionTags(tagsToAdd, result.Id);
                    return(RedirectToAction("SessionForActivity", "ActivitySummary", new { id = result.Id }));
                }
                else
                {
                    ModelState.AddModelError("",
                                             "Ett tillfälle med samma namn existerar redan till denna aktivitet!");
                }
            }

            ModelState.AddModelError("", "Lyckades inte skapa tillfället!");

            var allActivities          = _activityManager.GetAllActivities().OrderBy(n => n.Name).ToList();
            var allSessionParticipants = _personManager.GetAllParticipants().OrderBy(n => n.FirstName).ToList();
            var allHrPersons           = _personManager.GetAllHrPersons().OrderBy(n => n.FirstName).ToList();

            var selectedActivityId = allActivities.First().Id;

            ViewBag.AllActivities          = new SelectList(allActivities, "Id", "Name", selectedActivityId);
            ViewBag.AllSessionParticipants = new SelectList(
                allSessionParticipants,
                "Id",
                "FullName",
                allSessionParticipants.Skip(1).First().Id); // TODO: TAKE AWAY SKIP
            ViewBag.AllHrPersons = new SelectList(allHrPersons, "Id", "FullName");

            return(View(sessionVm));
        }