Пример #1
0
        public IActionResult IncrementViews(int id)
        {
            try
            {
                BlogPost blog = _context.Blogs.FirstOrDefault(b => b.Id == id);

                if (blog == null)
                {
                    throw new Exception($"No blog found! ID: {id}");
                }

                else
                {
                    blog.Views += 1;
                    _context.Entry(blog).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    _context.SaveChanges();

                    return(Ok(new BasicHttpResponse <object> {
                        Ok = true
                    }));
                }
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <object> {
                    Ok = false, Message = e.Message
                }));
            }
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebAssemblyDebugging();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            using (FivePeaksDbContext context = new FivePeaksDbContext())
            {
                context.Database.EnsureCreated();

                //Add admins if new db

                List <AdminUser> baseUsers = context.Admins.ToList();

                if (baseUsers.All(u => u.Username != "James"))
                {
                    context.Admins.Add(new AdminUser {
                        Username = "******", Password = "******"
                    });
                    context.SaveChanges();
                }
                if (baseUsers.All(u => u.Username != "Stuart"))
                {
                    context.Admins.Add(new AdminUser {
                        Username = "******", Password = "******"
                    });
                    context.SaveChanges();
                }
            }

            app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToPage("/BaseIndex");
            });
        }
Пример #3
0
        public IActionResult DeleteItem(int id)
        {
            try
            {
                LeaderboardItem blog = _context.Leaderboard.FirstOrDefault(b => b.Id == id);

                if (blog == null)
                {
                    throw new Exception($"No blog found! ID: {id}");
                }

                else
                {
                    _context.Remove(blog);
                    _context.SaveChanges();

                    return(Ok(new BasicHttpResponse <object> {
                        Ok = true
                    }));
                }
            }
            catch (Exception e)
            {
                return(Ok(new BasicHttpResponse <object> {
                    Ok = false, Message = e.Message
                }));
            }
        }