public Quote ToQuote(DatabaseContext context)
        {
            var quote = new Quote();
            if (Id != 0)
                quote = context.Quotes.Find(quote.Id);
            else
                quote.CreatedAt = DateTime.Now;
            quote.Text = Text;
            quote.Context = Context;

            if(AuthorId != null && !context.Users.Any(u => u.Id == AuthorId))
                throw new InvalidOperationException();
            if(SubmitterId != null && !context.Users.Any(u => u.Id == SubmitterId))
                throw new InvalidOperationException();

            User author = null;
            User submitter = null;
            if (AuthorId != null)
                author = context.Users.Find(AuthorId);
            if (SubmitterId != null)
                submitter = context.Users.Find(SubmitterId);
            quote.Submitter = submitter;
            quote.Author = author;
            quote.AlternateAuthor = AlternateAuthor;

            quote.Tags = context.Tags.Where(x => Tags.Contains(x.Text)).ToList();
            return quote;
        }
 public async Task<ActionResult> NewQuote(QuoteViewModel vm)
 {
     using (var dbContext = new DatabaseContext())
     {
         var quote = new Quote
         {
             Text = vm.Text,
             Submitter = dbContext.Users.Single(u => u.UserName == User.Identity.Name),
             CreatedAt = DateTime.Now,
             Tags = new List<Tag>()
         };
         var user = dbContext.Users.SingleOrDefault(x => x.UserName == vm.Author);
         if (user != null)
         {
             quote.Author = user;
         }
         else
         {
             quote.AlternateAuthor = String.IsNullOrWhiteSpace(vm.Author) ? "Anonymous" : vm.Author;
         }
         dbContext.Quotes.Add(quote);
         vm = new QuoteViewModel(quote);
         await dbContext.SaveChangesAsync();
         QuotesHub.NewQuote(quote);
     }
     return PartialView("_Quote", vm);
 }
 public QuoteViewModel(Quote quote)
 {
     Id = quote.Id;
     Text = quote.Text;
     Author = quote.Author != null ? quote.Author.UserName : quote.AlternateAuthor ?? "Anonymous";
     Tags = quote.Tags.Select(tag => new TagViewModel(tag)).ToList();
     CreatedAt = quote.CreatedAt;
     Submitter = quote.Submitter.UserName;
 }
        public QuoteViewModel(Quote q)
        {
            Id = q.Id;
            Text = q.Text;
            Context = q.Context;
            AuthorId = q.Author == null ? null : q.Author.Id;
            SubmitterId = q.Submitter == null ? null : q.Submitter.Id;

            AlternateAuthor = q.AlternateAuthor;
            CreatedAt = q.CreatedAt;
            Tags = q.Tags.Select(x => x.Text).ToList();
        }
Пример #5
0
        public static void NewQuote(Quote q)
        {
            var quoteVM = new QuoteViewModel(q);
            var page = Render("_Quote", "Quotes", quoteVM);
            _context.Value.Clients.All.newQuote(page);

            var wc = new WebClient();
            var jsonObject = new JObject();
            jsonObject["channel"] = "#banter";
            jsonObject["username"] = "******";
            jsonObject["text"] = $"> {q.Text}\n - {quoteVM.Author} {q.CreatedAt.Year}\n(Created by {q.Submitter.UserName} {q.CreatedAt.ToPrettyInterval()})";
            jsonObject["icon_emoji"] = ":kappa:";
            wc.UploadString(ConfigurationManager.ConnectionStrings["Slack-Banter"].ConnectionString, "POST", jsonObject.ToString());
        }