C
C#6mo ago
Sk

Trying to make it so when two of the same products are added to cart it increments quantity instead

Hello, im rying to make it so when two of the same products are added to cart it increments quantity instead of showing two of the same product with a quantity of one, heres my current code. Its very scuffed as its a test for my main project so it doesnt matter how ugly the code is haha https://paste.mod.gg/vrujthbplsay/3
BlazeBin - vrujthbplsay
A tool for sharing your source code with the world!
46 Replies
Pobiega
Pobiega6mo ago
this line if (_cartService.GetCartItems().Any(cartItem => cartItem.Equals(productToAdd))) doesnt do what you think it does I suggest you use a SingleOrDefault and match the ID instead of using Equals
Sk
Sk6mo ago
how would be best to do that?
Pobiega
Pobiega6mo ago
Try it yourself, see what you come up with. If you can't make it work, show us what you tried.
Sk
Sk6mo ago
var existingProduct = _cartService.GetCartItems().SingleOrDefault(cartItem => cartItem.Equals(productToAdd));

if (existingProduct != null)
{
// Product already exists in the cart, increase its quantity
existingProduct.Quantity++;
existingProduct.Name = "Test1";
}
else
{
// Product does not exist in the cart, add it with quantity 1
productToAdd.Quantity = 1;
productToAdd.Name = "Test2";
_cartService.AddToCart(productToAdd);
}
var existingProduct = _cartService.GetCartItems().SingleOrDefault(cartItem => cartItem.Equals(productToAdd));

if (existingProduct != null)
{
// Product already exists in the cart, increase its quantity
existingProduct.Quantity++;
existingProduct.Name = "Test1";
}
else
{
// Product does not exist in the cart, add it with quantity 1
productToAdd.Quantity = 1;
productToAdd.Name = "Test2";
_cartService.AddToCart(productToAdd);
}
i tried a few versions of this but to no avail
Pobiega
Pobiega6mo ago
Stop using Equals what do you think Equals does for two custom defined objects? I'm assuming you did not implement IEquatable on your CartItem class
Sk
Sk6mo ago
no, i implemeted it earlier but couldnt get it to work so i got rid of it
Pobiega
Pobiega6mo ago
Look at this
public record Product(int Id, string Name);

public class CartItem(Product product, int count)
{
public Product Product { get; init; } = product;
public int Count { get; set; } = count;
}

public class Cart
{
private readonly List<CartItem> _items = [];

public void Add(Product product)
{
var cartItem = _items.SingleOrDefault(x => x.Product.Id == product.Id);

if (cartItem == null)
{
_items.Add(new CartItem(product, 1));
return;
}

cartItem.Count++;
}
}
public record Product(int Id, string Name);

public class CartItem(Product product, int count)
{
public Product Product { get; init; } = product;
public int Count { get; set; } = count;
}

public class Cart
{
private readonly List<CartItem> _items = [];

public void Add(Product product)
{
var cartItem = _items.SingleOrDefault(x => x.Product.Id == product.Id);

if (cartItem == null)
{
_items.Add(new CartItem(product, 1));
return;
}

cartItem.Count++;
}
}
Sk
Sk6mo ago
what about it?
Pobiega
Pobiega6mo ago
Well, do you see how it does roughly the same thing but with a lot less code?
Sk
Sk6mo ago
yeah, does it create a new product in the code though, thats what im struggling most with, i can kind of do it if i am creating a new product in the if statement but because i already initialized all my products i find it much harder. but i see what you mean, lots of it is combined into the same lines
Pobiega
Pobiega6mo ago
var computer = new Product(2, "Computer");
var monitor = new Product(1, "Monitor");

var cart = new Cart();

cart.Add(computer);
cart.Add(computer);
cart.Add(monitor);
cart.Add(monitor);

Console.WriteLine(cart);
var computer = new Product(2, "Computer");
var monitor = new Product(1, "Monitor");

var cart = new Cart();

cart.Add(computer);
cart.Add(computer);
cart.Add(monitor);
cart.Add(monitor);

Console.WriteLine(cart);
thats what my test code looks like and it prints...
2x Computer
2x Monitor
2x Computer
2x Monitor
what Product looks like doesnt really matter, in this case its just an Id and a Name
Sk
Sk6mo ago
ah i see
Pobiega
Pobiega6mo ago
so if you find the product via the ID before adding or not doesnt really matter
Sk
Sk6mo ago
i added my other view where the button is pressed to add the item to the cart, only the first button is set up but does that look okay https://paste.mod.gg/ebnxxfmzfbrc/5
BlazeBin - ebnxxfmzfbrc
A tool for sharing your source code with the world!
Pobiega
Pobiega6mo ago
? I don't do MVC, so I'm not looking at the cshtml stuff
Sk
Sk6mo ago
oh, sorry i was just thinking it may be a bit of the problem but nvm
Pobiega
Pobiega6mo ago
how so? I mean, if your action is being called with invalid values, thats a problem yes but surely you used the debugger to make sure that isnt the case? :p
Sk
Sk6mo ago
i was just wondering as i had to use chatgpt for most of that so its hard for me to check myself
Pobiega
Pobiega6mo ago
well uhh you're using $.ajax which is almost definately not recommended these days, and your controllers "AddToCart" action is NOT set up to be an API endpoint so no, this is not correct
Sk
Sk6mo ago
im mainly making this project as something to go off for reference as its my first proper asp.net "project" so im trying to make it, even partly with chatgpt & tutorials, then use it for reference for my next projects
Pobiega
Pobiega6mo ago
an ajax request is done without a pageload event, meaning you cant return a redirect as the result
Sk
Sk6mo ago
would that have anything to do with messing up my add to cart oh
Pobiega
Pobiega6mo ago
yeah you seem to have not grasped the flow of a website app so at its core, ASP.NET MVC is purely serversided and serves server side rendered HTML from its controller endpoints but you've then added javascript to do XHR (ajax is the old way to do this), which is client side. That itself is fine, but your entire codebase must be written in a way to support this some endpoints would serve HTML, some would be API endpoints intended to be hit with XHR requests
Sk
Sk6mo ago
being honest i just used chatgpt for that, as its my first real asp.net "project" i tried to get some kind of guidance and just followed chatgpts advice blindly
Pobiega
Pobiega6mo ago
How do you expect to learn if you just chat GPT everything? I have no interest in reviewing or fixing AI written code. Good luck.
Sk
Sk6mo ago
what would you best recommend for learning? i cant just pick up the keyboard and start writing things i dont know, i wish i could but i cant, i watch youtube videos but there isnt an infinite source of videos to watch that arent just mindlessly copying. is there something you would recommend ?
Sk
Sk6mo ago
yeah but most of theirs are for blazor
Pobiega
Pobiega6mo ago
Some, not most. They cover web api, razor pages and even MVC to some extent. but yeah, MVC is "the old way", and not really common these days
Sk
Sk6mo ago
all jobs around me seem to be looking for MVC but yeah like you say, all the learning material seems to be ancient, at least 7 years+ for the most part
Pobiega
Pobiega6mo ago
Weird. I've worked with ASP.NET for 10+ years and not once done an MVC project
Sk
Sk6mo ago
which do you use?
Pobiega
Pobiega6mo ago
almost exclusively web apis frontends these days are usually so involved they need to be their own apps, and javascript is still king there I've done a razor page app or two as well, when we just wanted a quick-and-dirty admin panel But don't misunderstand me, MVC is fine as a technology. Its just a bit dated. it still works fine, especially once you are aware of the limitations to pure SSR and "how to get around it"
Sk
Sk6mo ago
is web api the most popular out of the others would you say?
Pobiega
Pobiega6mo ago
Its what I see most, yes. its different thou, its not a full app you make with it, just an API that is then consumed by some other app, maybe a javascript frontend, or a blazor app, or.. something else. can be almost anything
Sk
Sk6mo ago
okay, i may look into switching from MVC to web api as MVC material is all ancient
Pobiega
Pobiega6mo ago
you might want to look at razor pages, if you still want to make a full web app its very similar to MVC and 90-95% of what you learn carries over directly
Sk
Sk6mo ago
okay, im planning on learning JS/TS after c#, is that a conflict of interests do you think?
Pobiega
Pobiega6mo ago
not really? C# for backend and JS/TS for frontend is a very common tech choice
Sk
Sk6mo ago
yeah that was my plan
Pobiega
Pobiega6mo ago
you'd use C# and ASP.NET to write a web api, then access that API from a React/Angular/Vue/Svelte/Solid/Whatever javascript app
Sk
Sk6mo ago
would that work with razor? soryr if these are VERY stupid questions
Pobiega
Pobiega6mo ago
no, you'd not use either razor or MVC those are for serving HTML meaning you'd not really mix that with a "classic" js frontend I suppose you could mix it with something like htmx, but thats kinda niche still
Sk
Sk6mo ago
so if im planning on learn js/ts after its best to go the web api route ?
Pobiega
Pobiega6mo ago
they go well together, yes I don't really see a benefit to learning MVC if you plan on dumping it to use JS/TS immediately afterwards
Sk
Sk6mo ago
okay, thank you for all your advice and your patience with my stupid questions, its been really helpful talking to you! i think ill go the web api route then learn js/ts after