Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Context")] Entry entry, int[] categoriesId)
        {
            if (id != entry.Id)
            {
                return(NotFound());
            }

            Entry dbEntry = _context.Entries.AsNoTracking().FirstOrDefault(o => o.Id == id);

            entry.Created = dbEntry.Created;

            List <Category> categories = await _context.Categories.ToListAsync();

            categoriesId = categoriesId.Distinct().ToArray();

            foreach (Category category in categories)
            {
                EntryCategory entryCategory = _context.EntryCategories.FirstOrDefault(o => o.CategoryId == category.Id && o.EntryId == entry.Id);
                if (categoriesId.Any(o => o == category.Id)) // Relation should be kept / created
                {
                    if (entryCategory == null)               // If the relation doesn't exists, create it
                    {
                        _context.Add(new EntryCategory
                        {
                            Category = category,
                            Entry    = entry
                        });
                    }
                }
                else // Relation should be removed
                {
                    if (entryCategory != null) // If the relation exists, remove it
                    {
                        _context.Remove(entryCategory);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(entry);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EntryExists(entry.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(entry));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddCategoryToEntry(int id, [FromBody] Models.Category category, [FromServices] ApplicationDbContext db)
        {
            var entry = await db.Entries.SingleOrDefaultAsync(i => i.Id == id);

            if (entry == null)
            {
                return(NotFound("Entry not found"));
            }
            var dbCat = await db.Categories.SingleOrDefaultAsync(i => i.CategoryName.Equals(category.Name, StringComparison.InvariantCultureIgnoreCase));

            if (dbCat == null)
            {
                dbCat = new Data.Category()
                {
                    CategoryDescription = category.Description, CategoryName = category.Name
                };
                db.Categories.Add(dbCat);
                // if the category is new, there is no way it can exist in the entrycategory table, defer saving here til after EC is added.
            }
            var ec = await db.EntryCategories.SingleOrDefaultAsync(e => e.CategoryId == dbCat.Id && e.EntryId == entry.Id);

            if (ec == null)
            {
                ec = new EntryCategory()
                {
                    Category = dbCat, Entry = entry
                };
                db.EntryCategories.AddRange(ec);
                await db.SaveChangesAsync();
            }

            return(Created("", dbCat));
        }
Ejemplo n.º 3
0
        public async Task <Unit> Handle(UpdateEntryRequest request, CancellationToken cancellationToken)
        {
            var currentEntry = await _context.Entries
                               .FindAsync(request.EntryId)
                               .ConfigureAwait(false);

            if (currentEntry is null)
            {
                throw new EntityNotFoundException("User");
            }

            var readingTime = currentEntry.ReadingTime;

            if (request.ReadingTime == TimeSpan.MinValue)
            {
                readingTime = currentEntry.ReadingTime;
            }

            var entryCategories = currentEntry.EntryCategories;

            if (request.Categories.Any())
            {
                entryCategories.Clear();
                foreach (var item in request.Categories)
                {
                    var entryCategory = new EntryCategory
                    {
                        EntryId    = currentEntry.EntryId,
                        CategoryId = item.Id
                    };
                    entryCategories.Add(entryCategory);
                }
            }

            var entity = new Entry
                         (
                currentEntry.EntryId,
                request.Title ?? currentEntry.Title,

                readingTime,
                request.Content,

                entryCategories
                         );

            _context.Entries.Update(entity);
            await _context.SaveChangesAsync().ConfigureAwait(false);

            await _mediator.Publish(
                new EntryUpdated
            {
                EntryId = entity.EntryId,
                UserId  = entity.UpdatedBy
            }, cancellationToken)
            .ConfigureAwait(false);

            return(Unit.Value);
        }
Ejemplo n.º 4
0
        public LogEntry(DateTime time, string message, string[] tags = null, object sender = null, EntryCategory category = EntryCategory.Information)
        {
            Contract.Requires(message != null);
            Contract.Requires(time != null);

            this.tags = tags;

            if (tags != null)
            {
                for (int i = 0; i < this.tags.Length; i++)
                    this.tags[i] = this.tags[i].ToUpper();
            }

            this.message = message;

            if (sender != null)
                this.sender = sender.ToString() + " (" + sender.GetHashCode().ToString() + ")";

            this.category = category;
            this.time = time;
        }
Ejemplo n.º 5
0
        public static void Init(BlogDbContext context)
        {
            return;

            context.Database.EnsureCreated();

            if (context.BlogEntries.Any())
            {
                return;
            }

            var categories = new Category[]
            {
                new Category()
                {
                    Name = "Sports", Color = "darkgreen"
                },
                new Category()
                {
                    Name = "Politics", Color = "darkblue"
                },
                new Category()
                {
                    Name = "WebDev", Color = "brown"
                },
                new Category()
                {
                    Name = "Science", Color = "midnightblue"
                },
                new Category()
                {
                    Name = "Parenthood", Color = "darkred"
                },
            };

            foreach (var c in categories)
            {
                context.BlogCategories.Add(c);
            }
            context.SaveChanges();

            var entries = new Entry[]
            {
                new Entry("Uno", "Here's text No. 1 for my blog initializer. It gets categories 'Sports,' 'Politics,' and 'WebDev.'", DateTime.Now),
                new Entry("Dos", "Here's text No. 2. Init me. This one's in 'Sports.'", DateTime.Now),
                new Entry("Tres", "Here's 3. It's in 'Sports' too.", DateTime.Now),
                new Entry("Cuatro", "Here's No. 4, which is a family entry. So, 'Parenthood.'", DateTime.Now),
                new Entry("Cinco", "NUM-BER-FIVE-IS-A-LIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIVE! No specific categories, however. And I guess 'Science' just stays empty. For now.", DateTime.Now),
            };

            foreach (var e in entries)
            {
                context.BlogEntries.Add(e);
            }
            context.SaveChanges();

            var entryCategoriess = new EntryCategory[]
            {
                CreateEntryCategory(context, "Uno", "Sports"),
                CreateEntryCategory(context, "Uno", "Politics"),
                CreateEntryCategory(context, "Uno", "WebDev"),
                CreateEntryCategory(context, "Dos", "Sports"),
                CreateEntryCategory(context, "Tres", "Sports"),
                CreateEntryCategory(context, "Cuatro", "Parenthood"),
            };

            foreach (var ec in entryCategoriess)
            {
                context.BlogEntryCategories.Add(ec);
            }
            context.SaveChanges();
        }
        public virtual void Log(string message, string[] tags = null, object sender = null, EntryCategory category = EntryCategory.Information)
        {
            LogEntry entry = new LogEntry(DateTime.Now, message, tags, sender, category);

            Log(entry);
        }
Ejemplo n.º 7
0
 public KnownTechEntryAdd(EntryCategory category, NitroxTechType techType, bool verbose)
 {
     Category = category;
     TechType = techType;
     Verbose  = verbose;
 }
Ejemplo n.º 8
0
 public static Color Convert(EntryCategory value)
 {
     return((Color)App.Current.Resources["EntryCategoryColor" + value]);
 }