예제 #1
0
        public bool InsertCompetitionEntry(CompetitionEntry entry, out string errorMessage)
        {
            errorMessage = "";

            try
            {
                if (entry.FavouriteWatermelonPlace != null)
                {
                    var wordCount = entry.FavouriteWatermelonPlace.Split(' ').Count();
                    if (wordCount > 100)
                    {
                        errorMessage = "Too many words entered";
                        return(false);
                    }
                }
                _competitionEntryRepository.Insert(entry);
                _unitOfWork.SaveChanges();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return(false);
            }

            return(true);
        }
예제 #2
0
        public ActionResult CompetitionEntry([Bind(Include = "CompetitionEntryId,FullName,Email,Gender,FavouriteWatermelonPlace,TermsAndConditionsAccepted")] CompetitionEntry competitionEntry)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string errorMessage = "";
                    if (_competitionEntryService.InsertCompetitionEntry(competitionEntry, out errorMessage))
                    {
                        var messageBody = _messageFormatter.FormatMessage(competitionEntry);
                        _emailService.EmailMessage(competitionEntry.Email, "You have been entered", messageBody);
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, errorMessage);
                        return(View(competitionEntry));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, "An error occured trying to create your entry");
                    return(View("CompetitionEntry", competitionEntry));
                }
                return(RedirectToAction("Thankyou"));
            }

            return(View("CompetitionEntry", competitionEntry));
        }
        public void EmptyEntryReturnsInvalidModelState()
        {
            CompetitionEntry entry  = new CompetitionEntry();
            ViewResult       actual = _controller.CompetitionEntry(entry) as ViewResult;

            Assert.IsNotNull(actual);
            Assert.AreEqual(false, actual.ViewData.ModelState.IsValid);
        }
예제 #4
0
        public void EmptyEntryReturnsCorrectResult()
        {
            IMessageFormatter _formatter = new MessageFormatter();
            CompetitionEntry  entry      = new CompetitionEntry();

            var actual   = _formatter.FormatMessage(entry);
            var expected = "Name: \nEmail: \nGender: Male\nFavourite Place To Eat Watermellon: \nTerms and conditions: Not Accepted\n";

            Assert.AreEqual(expected, actual);
        }
예제 #5
0
        public void OneHundredWordsReturnsTrue()
        {
            string errorMessage;
            var    entry = new CompetitionEntry();

            entry.FavouriteWatermelonPlace = "This is a free online calculator which counts the number of words or units in a text. Authors writing your book, pupils working on your essay, self-employed word smiths, teachers, translators, professors, or simply curious individuals: please feel free to use this tool to count the number of words in your document. It will also be useful for database, grammatical research, translation breakdowns, dictionary creation, commercial writing, etc.This is a free online calculator which counts the number of words or units in a text. It will also be useful for database, grammatical research, translation breakdowns, dictionary creation, commercial writing, etc.";
            var actual = _service.InsertCompetitionEntry(entry, out errorMessage);

            Assert.AreEqual(true, actual);
            Assert.AreEqual("", errorMessage);
        }
예제 #6
0
        public void LessThan100WordsReturnsFalse()
        {
            string errorMessage;
            var    entry = new CompetitionEntry();

            entry.FavouriteWatermelonPlace = "This is a free online calculator which counts the number of words or units in a text";
            var actual = _service.InsertCompetitionEntry(entry, out errorMessage);

            Assert.AreEqual(true, actual);
            Assert.AreEqual("", errorMessage);
        }
        public void NonExistingEmailReturnsFalse()
        {
            IList <CompetitionEntry> fakeData = new List <CompetitionEntry>();
            var entry = new CompetitionEntry();

            entry.Email = "*****@*****.**";
            fakeData.Add(entry);
            _repo.Setup(item => item.GetAll()).Returns(fakeData);

            var actual = _service.EntryAlreadyExists("*****@*****.**");

            Assert.AreEqual(false, actual);
        }
예제 #8
0
        public void ExceptionReturnsFalse()
        {
            string errorMessage;
            var    entry = new CompetitionEntry();

            _repo.When(r => r.Insert(Arg.Any <CompetitionEntry>()))
            .Do(r => { throw new Exception("Exception Thrown"); });

            var actual = _service.InsertCompetitionEntry(entry, out errorMessage);

            Assert.AreEqual(false, actual);
            Assert.AreEqual("Exception Thrown", errorMessage);
        }
예제 #9
0
        public void ExistingEmailReturnsTrue()
        {
            IList <CompetitionEntry> fakeData = new List <CompetitionEntry>();
            var entry = new CompetitionEntry();

            entry.Email = "*****@*****.**";
            fakeData.Add(entry);
            _repo.GetAll().Returns(fakeData);

            var actual = _service.EntryAlreadyExists("*****@*****.**");

            Assert.AreEqual(true, actual);
        }
        public void InvalidEntryReturnsToCompetitionEntryView()
        {
            CompetitionEntry entry = new CompetitionEntry();
            var errorMessage       = "";

            _competitionEntryService.InsertCompetitionEntry(Arg.Any <CompetitionEntry>(), out errorMessage).Returns(true);
            _controller.ModelState.AddModelError("Model Error", "Model Error");

            ViewResult actual = _controller.CompetitionEntry(entry) as ViewResult;

            Assert.IsNotNull(actual);
            Assert.IsNotNull(actual.Model);
            Assert.AreEqual("CompetitionEntry", actual.ViewName);
        }
        public void ExceptionReturnsFalse()
        {
            string errorMessage;
            var    entry = new CompetitionEntry();

            _repo.Setup(item => item.Insert(It.IsAny <CompetitionEntry>()))
            .Throws(new Exception("Exception Thrown"));

            var actual = _service.InsertCompetitionEntry(entry, out errorMessage);

            Assert.AreEqual(false, actual);
            Assert.AreEqual("Exception Thrown", errorMessage);
            _repo.Verify(foo => foo.Insert(It.IsAny <CompetitionEntry>()), Times.Once());
        }
예제 #12
0
        /// <summary>
        /// Convert a DB-CompetitionEntry to the PES format.
        /// </summary>
        /// <param name="sourceCompetitionEntry"></param>
        /// <returns></returns>
        public static Task <PESCompetitionEntry> ConvertBack(CompetitionEntry sourceCompetitionEntry)
        {
            PESCompetitionEntry converted = new PESCompetitionEntry()
            {
                Position      = sourceCompetitionEntry.DatabasePosition,
                Id            = System.Convert.ToUInt32(sourceCompetitionEntry.SourceId),
                TeamId        = System.Convert.ToUInt32(sourceCompetitionEntry.TeamId),
                CompetitionId = System.Convert.ToUInt32(sourceCompetitionEntry.CompetitionId),
                Group         = (uint)sourceCompetitionEntry.Group,
                Order         = (byte)sourceCompetitionEntry.Order
            };

            return(Task.FromResult(converted));
        }
예제 #13
0
        public void CompletedEntryReturnsCorrectResult()
        {
            IMessageFormatter _formatter = new MessageFormatter();
            CompetitionEntry  entry      = new CompetitionEntry();

            entry.FullName = "Bob Smith";
            entry.Email    = "*****@*****.**";
            entry.Gender   = false;
            entry.FavouriteWatermelonPlace   = "In the bath";
            entry.TermsAndConditionsAccepted = false;

            var actual   = _formatter.FormatMessage(entry);
            var expected = "Name: Bob Smith\nEmail: [email protected]\nGender: Male\nFavourite Place To Eat Watermellon: In the bath\nTerms and conditions: Not Accepted\n";

            Assert.AreEqual(expected, actual);
        }
예제 #14
0
        /// <summary>
        /// Converts a PES team to the DB format.
        /// </summary>
        /// <param name="sourceCompetitionEntry"></param>
        /// <returns></returns>
        public static Task <CompetitionEntry> Convert(PESCompetitionEntry sourceCompetitionEntry)
        {
            var guid = Guid.NewGuid().ToString();
            CompetitionEntry converted = new CompetitionEntry()
            {
                CompetitionEntryId = guid,
                SourceId           = sourceCompetitionEntry.Id.ToString(),
                DatabasePosition   = sourceCompetitionEntry.Position,
                TeamId             = sourceCompetitionEntry.TeamId.ToString(),
                CompetitionId      = sourceCompetitionEntry.CompetitionId.ToString(),
                Group = (int)sourceCompetitionEntry.Group,
                Order = sourceCompetitionEntry.Order
            };

            return(Task.FromResult(converted));
        }
예제 #15
0
        public ActionResult SubmitEntry(string auth, bool wantsToJoin)
        {
            if (!ValidateAuthToken(auth))
            {
                return(Json(new { kind = "display", text = "Sorry, your session has timed out. Please refresh the page and try again." }));
            }

            var c = new CompetitionEntry();

            c.UpdateFromRequest();
            c.UserIPAddress = Security.UserIpV4Address();
            c.Save();

            string thanksGeneral = TextBlockCache.GetRich("Competition Thanks - General").BodyTextHtml;

            return(Json(new { kind = "display", text = thanksGeneral }));
        }
예제 #16
0
        public async Task <IActionResult> SubmitEntry([FromBody] CompetitionEntryRequest entry)
        {
            // Sanitise the input
            entry.Email = entry.Email.Trim();

            var newEntry = new CompetitionEntry
            {
                Email     = entry.Email,
                Answer    = entry.Answer,
                Created   = DateTime.UtcNow,
                IPAddress = HttpContext.Connection.RemoteIpAddress ?? HttpContext.Connection.LocalIpAddress
            };

            await _bufferBlock.SendAsync(newEntry);

            return(Ok());
        }
        public void CompleteEntryRedirectsToThankyou()
        {
            CompetitionEntry entry      = new CompetitionEntry();
            var errorMessage            = "";
            var competitionEntryService = Substitute.For <ICompetitionEntryService>();
            var emailService            = Substitute.For <IEmailService>();
            var messageFormatter        = Substitute.For <IMessageFormatter>();
            var controller = new WatermelonController(competitionEntryService, emailService, messageFormatter);

            competitionEntryService.InsertCompetitionEntry(Arg.Any <CompetitionEntry>(), out errorMessage)
            .Returns(x =>
            {
                x[1] = "";
                return(true);
            });
            controller.ModelState.Clear();

            var actual = controller.CompetitionEntry(entry) as RedirectToRouteResult;

            Assert.IsNotNull(actual);
            Assert.AreEqual("Thankyou", actual.RouteValues["action"]);
        }
        public async Task <CompetitionEntry> AddEntryAsync(int competitionId, CompetitionEntry competition)
        {
            // convert to entry
            var entryDto    = _mapper.Map <Entry>(competition);
            var dtoMetadata = entryDto.Metadata.ToObject <EntryMetadata>();

            // set the expiry time.
            dtoMetadata = await SetEntryDataExpiryTime(competitionId, dtoMetadata);

            entryDto.Metadata = JObject.FromObject(dtoMetadata);

            var result = await _defaultEntryService.AddEntryAsync(competitionId, entryDto);

            if (_defaultEntryService.HasErrors)
            {
                Errors    = _defaultEntryService.Errors; // just map any errors.
                HasErrors = true;
            }

            var competitionResult = _mapper.Map <CompetitionEntry>(result);

            return(competitionResult);
        }
        public string FormatMessage(CompetitionEntry entry)
        {
            StringBuilder messageBody = new StringBuilder();

            messageBody.Append("Name: ");
            messageBody.Append(entry.FullName);
            messageBody.Append("\n");
            messageBody.Append("Email: ");
            messageBody.Append(entry.Email);
            messageBody.Append("\n");
            messageBody.Append("Gender: ");
            if (entry.Gender == false)
            {
                messageBody.Append("Male");
            }
            else
            {
                messageBody.Append("Female");
            }
            messageBody.Append("\n");
            messageBody.Append("Favourite Place To Eat Watermellon: ");
            messageBody.Append(entry.FavouriteWatermelonPlace);
            messageBody.Append("\n");
            messageBody.Append("Terms and conditions: ");
            if (entry.TermsAndConditionsAccepted == true)
            {
                messageBody.Append("Accepted");
            }
            else
            {
                messageBody.Append("Not Accepted");
            }
            messageBody.Append("\n");

            return(messageBody.ToString());
        }
        public async Task <IActionResult> AddEntryAsync([FromRoute] int competitionId, CompetitionEntry entryDto)
        {
            // add event to game
            var entryAdded = await _entryService.AddEntryAsync(competitionId, entryDto);

            if (_entryService.HasErrors)
            {
                return(new BadRequestObjectResult(new ApiError(_entryService.Errors)));
            }

            return(Ok(entryAdded));
        }