public async Task <IActionResult> Create([Bind(("ID,theme,sendertUserEmail,senderUserFullName,recipientUserEmail,sentencePrefix,fullSentence,sentenceSuffix,image,publishedToFacebook"))] GreetingCard greetingCard)
        {
            if (ModelState.IsValid)
            {
                var recipientUser = _context.Users.FirstOrDefaultAsync(m => m.Email == greetingCard.recipientUserEmail).Result;
                if (recipientUser != default)
                {
                    var recipientUserFirstName = recipientUser.FirstName;
                    var recipientUserLastName  = recipientUser.LastName;
                    greetingCard.recipientUserFullName = recipientUserFirstName + " " + recipientUserLastName;
                    greetingCard.fullSentence          = greetingCard.sentencePrefix + ", " + recipientUserFirstName + "! " + greetingCard.sentenceSuffix;
                }
                else
                {
                    greetingCard.recipientUserFullName = greetingCard.recipientUserEmail;
                    greetingCard.recipientUserEmail    = "*****@*****.**";
                    greetingCard.fullSentence          = greetingCard.sentencePrefix + ", " + greetingCard.recipientUserFullName + "! " + greetingCard.sentenceSuffix;
                }

                Image selectedImage = _context.Images.FirstOrDefaultAsync(m => m.imageName == greetingCard.image).Result;
                if (selectedImage != default)
                {
                    greetingCard.image     = selectedImage.imagePath;
                    greetingCard.imageID   = selectedImage.ID;
                    greetingCard.imageName = selectedImage.imageName;
                }

                var sentence = _context.SentimentSentences.FirstOrDefaultAsync(m => m.prefix == greetingCard.sentencePrefix && m.suffix == greetingCard.sentenceSuffix).Result;
                if (sentence == default)
                {
                    var sentenceBySuffix = _context.SentimentSentences.FirstOrDefaultAsync(m => m.suffix == greetingCard.sentenceSuffix).Result;
                    if (sentenceBySuffix != default)
                    {
                        var newSentenceToDB = new SentimentSentence {
                            prefix             = greetingCard.sentencePrefix, suffix = greetingCard.sentenceSuffix, complexity = sentenceBySuffix.complexity,
                            recipientUserEmail = greetingCard.recipientUserEmail, recipientUserFirstName = greetingCard.recipientUserFullName, theme = greetingCard.theme
                        };
                        _context.SentimentSentences.Add(newSentenceToDB);
                    }
                }

                _context.Add(greetingCard);
                await _context.SaveChangesAsync();

                if (greetingCard.publishedToFacebook)
                {
                    postToFacebook(greetingCard.senderUserFullName, greetingCard.sentencePrefix, greetingCard.recipientUserFullName, greetingCard.sentenceSuffix);
                }
                return(RedirectToAction(nameof(Index)));
            }

            greetingCard.ImagesDB             = _context.Images;
            greetingCard.SentimentSentencesDB = _context.SentimentSentences;
            return(View(greetingCard));
        }
예제 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,theme,prefix,recipientUserEmail,recipientUserFirstName,suffix,complexity")] SentimentSentence sentimentSentence)
        {
            if (id != sentimentSentence.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var recipientUser = _context.Users.FirstOrDefaultAsync(m => m.Email == sentimentSentence.recipientUserEmail).Result;
                    if (recipientUser != default)
                    {
                        var recipientUserFirstName = recipientUser.FirstName;
                        sentimentSentence.recipientUserFirstName = recipientUserFirstName;
                    }
                    else
                    {
                        sentimentSentence.recipientUserFirstName = sentimentSentence.recipientUserEmail;
                        sentimentSentence.recipientUserEmail     = "*****@*****.**";
                    }

                    _context.Update(sentimentSentence);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SentimentSentenceExists(sentimentSentence.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(sentimentSentence));
        }
예제 #3
0
        public async Task <IActionResult> Create([Bind(("ID,theme,prefix,recipientUserEmail,suffix,complexity"))] SentimentSentence sentimentSentence)
        {
            if (ModelState.IsValid)
            {
                var recipientUser = _context.Users.FirstOrDefaultAsync(m => m.Email == sentimentSentence.recipientUserEmail).Result;
                if (recipientUser != default)
                {
                    var recipientUserFirstName = recipientUser.FirstName;
                    sentimentSentence.recipientUserFirstName = recipientUserFirstName;
                }
                else
                {
                    sentimentSentence.recipientUserFirstName = sentimentSentence.recipientUserEmail;
                    sentimentSentence.recipientUserEmail     = "*****@*****.**";
                }

                _context.Add(sentimentSentence);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(sentimentSentence));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ID,theme,sendertUserEmail,senderUserFullName,recipientUserEmail,sentencePrefix,fullSentence,sentenceSuffix,image,publishedToFacebook")] GreetingCard greetingCard)
        {
            if (id != greetingCard.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var recipientUser = _context.Users.FirstOrDefaultAsync(m => m.Email == greetingCard.recipientUserEmail).Result;
                    if (recipientUser != default)
                    {
                        var recipientUserFirstName = recipientUser.FirstName;
                        var recipientUserLastName  = recipientUser.LastName;
                        greetingCard.recipientUserFullName = recipientUserFirstName + " " + recipientUserLastName;
                        greetingCard.fullSentence          = greetingCard.sentencePrefix + ", " + recipientUserFirstName + "! " + greetingCard.sentenceSuffix;
                    }
                    else
                    {
                        greetingCard.recipientUserFullName = greetingCard.recipientUserEmail;
                        greetingCard.recipientUserEmail    = "*****@*****.**";
                        greetingCard.fullSentence          = greetingCard.sentencePrefix + ", " + greetingCard.recipientUserFullName + "! " + greetingCard.sentenceSuffix;
                    }

                    Image selectedImage = _context.Images.FirstOrDefaultAsync(m => m.imageName == greetingCard.image).Result;
                    if (selectedImage != default)
                    {
                        greetingCard.image     = selectedImage.imagePath;
                        greetingCard.imageID   = selectedImage.ID;
                        greetingCard.imageName = selectedImage.imageName;
                    }

                    var sentence = _context.SentimentSentences.FirstOrDefaultAsync(m => m.prefix == greetingCard.sentencePrefix && m.suffix == greetingCard.sentenceSuffix).Result;
                    if (sentence == default)
                    {
                        var sentenceBySuffix = _context.SentimentSentences.FirstOrDefaultAsync(m => m.suffix == greetingCard.sentenceSuffix).Result;
                        if (sentenceBySuffix != default)
                        {
                            var newSentenceToDB = new SentimentSentence
                            {
                                prefix                 = greetingCard.sentencePrefix,
                                suffix                 = greetingCard.sentenceSuffix,
                                complexity             = sentenceBySuffix.complexity,
                                recipientUserEmail     = greetingCard.recipientUserEmail,
                                recipientUserFirstName = greetingCard.recipientUserFullName,
                                theme = greetingCard.theme
                            };
                            _context.SentimentSentences.Add(newSentenceToDB);
                        }
                    }

                    _context.Update(greetingCard);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!GreetingCardsExists(greetingCard.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(greetingCard));
        }