public Order GetByID(int id)
 {
     using (var db = new SalesDBContext())
     {
         return(db.Orders.FirstOrDefault(o => o.ID == id));
     }
 }
Esempio n. 2
0
 public List <Customer> GetList()
 {
     using (var db = new SalesDBContext())
     {
         return(db.Customers.ToList());
     }
 }
 public List<OrderItem> GetListByOrderId(int orderId)
 {
     using (var db=new SalesDBContext())
     {
         return db.OrderItems.Where(i => i.OrderId == orderId).ToList();
     }
 }
Esempio n. 4
0
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        SalesDBContext db = new SalesDBContext();
        UserManager <ApplicationUser> userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));
        ApplicationDbContext          dbu         = new ApplicationDbContext();
        bool isUserAuthorized = base.AuthorizeCore(httpContext);

        string[]             permissions = Permissions.Split(',').ToArray();
        IEnumerable <string> perms       = permissions.Intersect(db.Permissions.Select(p => p.ActionName));
        List <IdentityRole>  roles       = new List <IdentityRole>();

        if (perms.Count() > 0)
        {
            foreach (var item in perms)
            {
                var currentUserId          = httpContext.User.Identity.GetUserId();
                var relatedPermisssionRole = dbu.Roles.Find(db.Permissions.Single(p => p.ActionName == item).RoleId).Name;
                if (userManager.IsInRole(currentUserId, relatedPermisssionRole))
                {
                    return(true);
                }
            }
        }
        return(false);
    }
 public UserProfile GetById(int id)
 {
     using (var db = new SalesDBContext())
     {
         return(db.UserProfiles.FirstOrDefault(u => u.ID == id));
     }
 }
 public List <UserProfile> GetList()
 {
     using (var db = new SalesDBContext())
     {
         return(db.UserProfiles.ToList());
     }
 }
Esempio n. 7
0
 public List <Unit> GetList()
 {
     using (var db = new SalesDBContext())
     {
         return(db.Units.ToList());
     }
 }
        public SalesControllerTests()
        {
            var optionBuilder = new DbContextOptions <SalesDBContext>();

            //var optionBuilder = new DbContextOptionsBuilder<SalesDBContext>().UseInMemoryDatabase();
            salesDBContext = new SalesDBContext(optionBuilder);
        }
Esempio n. 9
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, SalesDBContext salesDBContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            salesDBContext.Database.EnsureCreated();
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger Sales API");
                options.RoutePrefix = "";
            });
            app.UseAuthentication();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
            });
            app.UseDeveloperExceptionPage();
        }
 public OrderItem GetById(int id)
 {
     using (var db = new SalesDBContext())
     {
         return db.OrderItems.FirstOrDefault(i=>i.ID==id);
     }
 }
Esempio n. 11
0
        /// <summary>
        /// CSV Reader.
        /// </summary>
        /// <param name="file"></param>
        public void Reader(string file)
        {
            // Instance of Entity Framework Class;
            SalesDBContext db = new SalesDBContext();

            // Delete Data each time import the file
            db.Database.ExecuteSqlCommand("TRUNCATE TABLE Sales");

            try
            {
                //CSV PARSER
                using (TextFieldParser parser = new TextFieldParser(file, Encoding.GetEncoding("iso-8859-1")))
                {
                    parser.Delimiters = new string[] { "," };
                    int CurrentLine = 0;
                    while (!parser.EndOfData)
                    {
                        string[] columns = parser.ReadFields();
                        if (columns == null)
                        {
                            break;
                        }
                        //Making empty value as null
                        for (int i = 0; i < columns.Length; i++)
                        {
                            if (columns[i] == "")
                            {
                                columns[i] = null;
                            }
                        }
                        if (CurrentLine != 0)
                        {
                            var newSalesData = new Sales
                            {
                                DealNumber     = int.Parse(columns[0]),
                                DealershipName = columns[1],
                                CustomerName   = columns[2],
                                Vehicle        = columns[3],
                                Price          = decimal.Parse(columns[4].Replace(",", ".")),
                                Date           = DateTime.Parse(columns[5])
                            };

                            // Add Data to The Database
                            db.Sales.Add(newSalesData);
                            db.SaveChanges();
                        }
                        CurrentLine++;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //Find the vehicle that was sold the most often.
            SoldMost(db);
        }
Esempio n. 12
0
        public IActionResult Index()
        {
            SalesDBContext context = HttpContext.RequestServices.GetService(typeof(SalesDBContext)) as SalesDBContext;

            ViewData["Title"]      = "Anychart ASP.NET Core C# dashboard";
            ViewData["ChartTitle"] = "Top 5 sales";
            ViewData["ChartData"]  = JsonConvert.SerializeObject(context.GetTopSales());
            return(View(context.GetTopSales()));
        }
Esempio n. 13
0
        public async Task AddNewSale()
        {
            var context = new SalesDBContext(Config.GetConnection());
            IGenericRepositoryAsync <Sale> _repository = new GenericRepositoryAsync <Sale>(context);
            var         service = new GenericServiceAsync <Sale>(_repository);
            List <Sale> sales   = new List <Sale>
            {
                new Sale
                {
                    Id            = id,
                    Region        = "America do Sul",
                    Country       = "Argentina",
                    ItemType      = "Feijao",
                    SalesChannel  = "Online",
                    OrderPriority = "H",
                    OrderDate     = DateTime.Now.AddMonths(-7),
                    OrderId       = "555325",
                    ShipDate      = DateTime.Now.AddMonths(-4),
                    UnitsSold     = 1530,
                    UnitPrice     = 3367,
                    UnitCost      = 2123,
                    TotalRevenue  = 114.57M,
                    TotalCost     = 520,
                    TotalProfit   = 25350
                },

                new Sale
                {
                    Id            = id,
                    Region        = "Europa",
                    Country       = "Ireland",
                    ItemType      = "Tapioca",
                    SalesChannel  = "Online",
                    OrderPriority = "H",
                    OrderDate     = DateTime.Now.AddMonths(-22),
                    OrderId       = "2223",
                    ShipDate      = DateTime.Now.AddMonths(-13),
                    UnitsSold     = 53220,
                    UnitPrice     = 423,
                    UnitCost      = 2352,
                    TotalRevenue  = 1545.5M,
                    TotalCost     = 32460,
                    TotalProfit   = 32500
                }
            };

            foreach (var item in sales)
            {
                await service.Add(item).ConfigureAwait(false);
            }

            await service.SaveAsync();

            Assert.True(true);
        }
Esempio n. 14
0
        public async Task GetAllCountries()
        {
            var context = new SalesDBContext(Config.GetConnection());
            ISalesRepository _repository = new SalesRepository(context);
            var         salesService     = new SalesService(_repository);
            List <Sale> sales            = new List <Sale>();

            var lstCountries = await salesService.SelectDistinctCountries().ConfigureAwait(false);

            Assert.True(true);
        }
Esempio n. 15
0
        public StoresController(SalesDBContext context)
        {
            _context = context;
            if (_context.Store.Count() == 0)
            {
                // Create a new TodoItem if collection is empty,
                // which means you can't delete all TodoItems.

                _context.Store.Add(new Store {
                    Name = "Item1", Address = "main"
                });
                _context.SaveChanges();
            }
        }
Esempio n. 16
0
        public ProductController(SalesDBContext context)
        {
            _context = context;

            if (_context.Product.Count() == 0)
            {
                // Create a new TodoItem if collection is empty,
                // which means you can't delete all TodoItems.

                _context.Product.Add(new Product {
                    Name = "Item1", Price = 10
                });
                _context.SaveChanges();
            }
        }
        public CustomersController(SalesDBContext context)
        {
            _context = context;

            if (_context.Customer.Count() == 0)
            {
                // Create a new TodoItem if collection is empty,
                // which means you can't delete all TodoItems.

                _context.Customer.Add(new Customer {
                    Name = "Item1", Address = "Address 1"
                });
                _context.SaveChanges();
            }
        }
 public EditUserProfile GetEditUserProfileById(int id)
 {
     using (var db = new SalesDBContext())
     {
         var             userProfile    = db.UserProfiles.FirstOrDefault(u => u.ID == id);
         EditUserProfile editUseProfile = new EditUserProfile
         {
             FirstName = userProfile.FirstName,
             LastName  = userProfile.LastName,
             UserName  = userProfile.UserName,
             Email     = userProfile.Email,
             Address   = userProfile.Address
         };
         return(editUseProfile);
     }
 }
 public bool Create(UserProfile userProfile)
 {
     using (var db = new SalesDBContext())
     {
         try
         {
             db.UserProfiles.Add(userProfile);
             db.SaveChanges();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }
 public bool Create(Order order)
 {
     using (var db = new SalesDBContext())
     {
         try
         {
             db.Orders.Add(order);
             db.SaveChanges();
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }
 public bool Create(OrderItem orderItem)
 {
     using (var db = new SalesDBContext())
     {
         try
         {
             db.OrderItems.Add(orderItem);
             db.SaveChanges();
             return true;
         }
         catch
         {
             return false;
         }
     }
 }
Esempio n. 22
0
        public SalesController(SalesDBContext context)
        {
            _context = context;

            //   if (_context.Sales.Count() == 0)
            //   {
            //     // Create a new TodoItem if collection is empty,
            //     // which means you can't delete all TodoItems.

            //     _context.Sales.Add(new Sales
            //     {
            //       ProductId = 2,
            //       CustomerId = 4,
            //       StoreId = 2,
            //       DateSold = DateTime.Today
            //     });
            //     _context.SaveChanges();
            //   }
        }
Esempio n. 23
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, SalesDBContext salesDBContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            salesDBContext.Database.EnsureCreated();
        }
        public bool Delete(int id)
        {
            using (var db = new SalesDBContext())
            {
                try
                {
                    var q = db.OrderItems.FirstOrDefault(x => x.ID == id);
                    if (q != null)
                    {
                        db.OrderItems.Remove(q);
                        db.SaveChanges();
                    }

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
        public bool Update(OrderItem orderItem)
        {
            using (var db = new SalesDBContext())
            {
                try
                {
                    var q = db.OrderItems.FirstOrDefault(O => O.ID == orderItem.ID);
                    if (q != null)
                    {
                        q.Amount =orderItem.Amount;
                        q.UnitId = orderItem.UnitId;
                        db.SaveChanges();
                    }
                }
                catch
                {
                    return false;
                }

                return true;
            }
        }
        public bool Update(Order order)
        {
            using (var db = new SalesDBContext())
            {
                try
                {
                    var q = db.Orders.FirstOrDefault(O => O.ID == order.ID);
                    if (q != null)
                    {
                        q.OrderDate   = order.OrderDate;
                        q.TotalAmount = order.TotalAmount;
                        q.CustomerId  = order.CustomerId;
                        db.SaveChanges();
                    }
                }
                catch
                {
                    return(false);
                }

                return(true);
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Find the vehicle that was sold the most often.
        /// </summary>
        /// <param name="db"></param>
        internal void SoldMost(SalesDBContext db)
        {
            if (db.Sales.ToList() != null && db.Sales.ToList().Count > 0)
            {
                var soldMost = db.Sales.ToList()
                               .GroupBy(q => q.Vehicle)
                               .OrderByDescending(gp => gp.Count())
                               .Take(1)
                               .Select(g => g.Key).First();

                var soldMostVehicle = new Sales
                {
                    DealNumber     = 0,
                    DealershipName = string.Empty,
                    CustomerName   = string.Empty,
                    Vehicle        = string.Empty,
                    Price          = 0,
                    Date           = DateTime.Now,
                    SoldMost       = soldMost
                };
                db.Sales.Add(soldMostVehicle);
                db.SaveChanges();
            }
        }
        public bool Update(EditUserProfile editUserProfile)
        {
            using (var db = new SalesDBContext())
            {
                try
                {
                    var q = db.UserProfiles.FirstOrDefault(u => u.ID == editUserProfile.ID);
                    if (q != null)
                    {
                        q.FirstName = editUserProfile.FirstName;
                        q.LastName  = editUserProfile.LastName;
                        q.Email     = editUserProfile.Email;
                        q.Address   = editUserProfile.Address;
                        db.SaveChanges();
                    }
                }
                catch
                {
                    return(false);
                }

                return(true);
            }
        }
 public ProductsController(SalesDBContext context)
 {
     _context = context;
 }
Esempio n. 30
0
 public ManagersRepository(SalesDBContext context)
 {
     _db = context ?? throw new ArgumentNullException(nameof(context));
 }