keanacobarde / BEDuo

0 stars 0 forks source link

SETUP - CLASSES, TEST DATA, + DBCONTEXT #2

Closed keanacobarde closed 4 months ago

keanacobarde commented 4 months ago

User Story

I, as the developer, must create classes to represent the entities outlined within the ERD to all for Entity Framework to import said entities to PostgreSQL database.

Acceptance Criteria

ICOllections/Lists will need to be referenced between the following entities

Dependencies

Dev Notes

namespace Bangazon.Models { public class Product { public int Id { get; set; } [Required] public string Title { get; set; } public string Description { get; set; } public string ImageUrl { get; set; } public int QuantityAvailable { get; set; } public float Price { get; set; } public int SellerId { get; set; } public int CategoryId { get; set; } public ICollection Orders { get; set; } } }


Examples of utilizing accumulators to generate 'Totals' - CreekRiver: 
public decimal? TotalCost
{
    get
    {
        if (Campsite?.CampsiteType != null)
        {
            return Campsite.CampsiteType.FeePerNight * TotalNights + _reservationBaseFee;
        }
        return null;
    }
}
- DbContext Structure Example:
```chsarp
using Microsoft.EntityFrameworkCore;
using Bangazon.Models;

namespace Bangazon
{
    public class BangazonDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<PaymentType> PaymentTypes { get; set; }
        public DbSet<Category> Categories { get; set; }

        public BangazonDbContext(DbContextOptions<BangazonDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasData(new User[]
            { 
                new User
                {
                    Id = 1,
                    Username = "kcob",
                    FirstName = "Keana",
                    LastName = "Cobarde",
                    Email = "keanacobarde@gmail.com",
                    Address = "123 Witchy Lane",
                    IsSeller = true,
                    Uid = "Kqa5uPcWOgRIUtiXEBnJbP6VQXB3"
                },
                new User
                {
                    Id = 2,
                    Username = "keykey",
                    FirstName = "Kiki",
                    LastName = "Wiki",
                    Email = "keyzelgears@gmail.com",
                    Address = "124 Witchy Lane",
                    IsSeller = true,
                    Uid = "Kqa5uPcWOgRIUtiXEBnJbP6VQXB3"
                },
                new User
                {
                    Id = 3,
                    Username = "kira",
                    FirstName = "June",
                    LastName = "Bloom",
                    Email = "junejune22@gmail.com",
                    Address = "125 Witchy Lane",
                    IsSeller = false,
                    Uid = "Kqa5uPcWOgRIUtiXEBnJbP6VQXB3"
                }
            });

            modelBuilder.Entity<Product>().HasData(new Product[]
            {
                new Product
                {
                    Id = 1,
                    Title = "Laptop",
                    Description = "Powerful laptop for all your computing needs",
                    ImageUrl = "laptop_image_url.jpg",
                    QuantityAvailable = 50,
                    Price = 999.99f,
                    SellerId = 1,
                    CategoryId = 1,
                },
                new Product
                {
                    Id = 2,
                    Title = "Smartphone",
                    Description = "High-performance smartphone with advanced features",
                    ImageUrl = "smartphone_image_url.jpg",
                    QuantityAvailable = 100,
                    Price = 499.99f,
                    SellerId = 2,
                    CategoryId = 2,
                },
                new Product
                {
                    Id = 3,
                    Title = "Headphones",
                    Description = "Premium noise-canceling headphones for an immersive audio experience",
                    ImageUrl = "headphones_image_url.jpg",
                    QuantityAvailable = 30,
                    Price = 149.99f,
                    SellerId = 1,
                    CategoryId = 2,
                }
            });

            modelBuilder.Entity<Order>().HasData(new Order[]
            {
                new Order
                {
                    Id = 1,
                    CustomerId = 2,
                    PaymentId = 2,
                    IsOrderOpen = true,
                },

                new Order
                {
                    Id = 2,
                    CustomerId = 2,
                    PaymentId = 3,
                    IsOrderOpen = true,
                },
                new Order
                {
                    Id = 3,
                    CustomerId = 2,
                    PaymentId = 1,
                    IsOrderOpen = false,
                }
            });

            modelBuilder.Entity<PaymentType>().HasData(new PaymentType[]
            {

                new PaymentType
                {
                     Id = 1,
                     Name = "Credit",
                },

                new PaymentType
                {
                    Id = 2,
                    Name = "Debit Card",
                },

                new PaymentType
                {
                    Id = 3,
                    Name = "Cryptocurrency",
                },

            });

            modelBuilder.Entity<Category>().HasData(new Category[]
            {
                new Category
                {
                    Id = 1,
                    Name = "Samsung",
                },
                new Category
                {
                    Id = 2,
                    Name = "Apple",
                },
                new Category
                {
                    Id = 3,
                    Name = "Sony",
                },
            });

        }

    }
}

-Seeding Test Data Example
DBCONTEXT
```csharp
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasData(UsersData.Users);
            modelBuilder.Entity<Item>().HasData(ItemsData.Items);
            modelBuilder.Entity<Order>().HasData(OrdersData.Orders);
        }

DATA FILE

using HHPWsBe.Models;

namespace HHPWsBe.Data
{
    public class ItemsData
    {
        public static List<Item> Items = new List<Item>
        {
            new Item() { Id = 1, Name = "Margherita", Price = 8.99M },
            new Item() { Id = 2, Name = "Pepperoni", Price = 9.99M },
            new Item() { Id = 3, Name = "Vegetarian", Price = 10.99M },
            new Item() { Id = 4, Name = "Hawaiian", Price = 11.99M },
        };
    }
}

-Creating Modularized API Call Example PROGRAM.CS

ItemsAPI.Map(app);
OrderItemsAPI.Map(app);
OrdersAPI.Map(app);
UsersAPI.Map(app);
PaymentInfoAPIcs.Map(app);

API FILE

using HHPWsBe.Models;

namespace HHPWsBe.APIs
{
    public class ItemsAPI
    {
        public static void Map(WebApplication app)
        {
            app.MapGet("/items", (HHPWsDbContext db) =>
            {
               return db.Items.ToList();
            });

            app.MapDelete("/items/delete/{id}", (HHPWsDbContext db, int id) => 
            { 
                Item itemToDelete = db.Items.FirstOrDefault(i => i.Id == id);
                if (itemToDelete == null)
                {
                    return Results.NotFound();
                }
                db.Items.Remove(itemToDelete);
                db.SaveChanges();
                return Results.Ok(db.Items);
            });
        }
    }
}
keanacobarde commented 4 months ago

Added test data and required classes/models