How do I display my saved quantities? - c#

In my store I have an orders table that captures the orders made by each customer. I want to be able to show the user what quantity of an item they have in their cart. Here is my orders model:
public class Order
{
public int Id { get; set; }
public ApplicationUser Customer { get; set; }
public string ShirtCause {get; set;}
public DateTime CreateDate { get; set; }
public ShirtSize ShirtSize { get; set; }
public ShirtQuantity ShirtQuantity { get; set; }
public decimal Total { get; set; }
public string ShirtYear { get; set; }
public int OrderNum { get; set; }
public bool? OrderCompleted { get; set; }
public bool? RecievedShirt { get; set; }
}
Using code first ShirtQuantity is created in a table and is reflected in the column ShirtQuantity_Id. For reference purposes, here is my quantity model:
public class ShirtQuantity
{
public int Id { get; set; }
public int Name { get; set; }
}
The cart should be output and enumerated in the action with:
return View(db.Orders.ToList());
All except for the Quantity_Id will list when calling
#model IEnumerable<ProjectName.Models.Order>
in the view. How do I get these quantities to list? I have tried creating a view model combining orders and shirtQuantities and calling it in the view:
public class ShirtOrdersViewModel
{
public int ShirtSizeId { get; set; }
public IEnumerable<ShirtSize> ShirtSizes { get; set; }
public int ShirtQuantityId { get; set; }
public IEnumerable<ShirtQuantity> ShirtQuantities { get; set; }
public int ShirtId { get; set; }
public IEnumerable<Shirt> Shirts { get; set; }
public int FileId { get; set; }
public IEnumerable<File> Files { get; set; }
public int OrderId { get; set; }
public IEnumerable<Order> Orders { get; set; }
public decimal Total { get; set; }
}
and this also does not work.

Related

dotnet mvc relationships in post method

I have these tables: Sales, product, stock and salesProduct. I'm using a database first approach, they were created using EF scaffold. Sales table looks like this:
public partial class Sales
{
public Sales()
{
SalesProduct = new HashSet<SalesProduct>();
}
public int Id { get; set; }
public DateTime? CreationDate { get; set; }
public bool IndActive { get; set; }
public virtual ICollection<SalesProduct> SalesProduct { get; set; }
}
}
I need the post method on sales creation to create a SalesProduct object and add it to the database. I also need it to update the stock of that specific product.
Here are the other tables:
public partial class SalesProduct
{
public int Id { get; set; }
public int SaleId { get; set; }
public int ProductId { get; set; }
public decimal? Value { get; set; }
public bool IndActive { get; set; }
public virtual Produto IdProductNavigation { get; set; } = null!;
public virtual Vendum IdSalesNavigation { get; set; } = null!;
}
}
public partial class Stock
{
public int Id { get; set; }
public int IdProduct { get; set; }
public int? Quantitu { get; set; }
public bool IndActive { get; set; }
public virtual Product IdProductNavigation { get; set; } = null!;
}
}
How can i do that? I'm confused on how to deal with entity relationships in http methods!

AutoMapper not mapping child list of objects

I have a parent object(brand) with list of 5 child object(campaign):
Brand response = await _brandRepository.GetAsync(request.Id);
//other properties mapped fine, but Campaing is null..
var result = _mapper.Map<BrandDto>(response);
return result;
I have these definations for mapping:
CreateMap<Domain.Entity.Campaign, CampaignDto>();
CreateMap<Domain.Entity.Brand, BrandDto>();
objects:
public class BrandDto
{
public int Id { get; set; }
public string Name { get; set; }
public List<CampaignDto> Campaing { get; set; }
}
public class CampaignDto
{
public int Id { get; set; }
public int? ParentCampaignId { get; set; }
public int BrandId { get; set; }
public CampaignDto ParentCampaign { get; set; }
public BrandDto Brand { get; set; }
}
entities:
public class Brand : SqlEntityBase
{
public string Name { get; set; }
public List<Campaign> Campaign { get; set; } = new List<Campaign>();
}
public class Campaign : SqlEntityBase
{
public int? ParentCampaignId { get; set; }
public int BrandId { get; set; }
public Guid ImpressionId { get; set; }
public string Name { get; set; }
public EntryStatus Status { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public DateTime CreatedOn { get; set; }
public Brand Brand { get; set; }
public Campaign ParentCampaign { get; set; }
}
If I map child object manually it works fine, but its not cool
how can I make this proper way?

Map two lists to parent class and nested class

I have two lists, list of Accounts and list of Transactions that represent below model. Let's assume they look like this:
List<Response.Account> accounts;
List<Response.Transaction> transactions;
And the model:
public class Response
{
public List<Account> Accounts { get; set; } = new List<Account>();
public class Account
{
public string AccountNumber { get; set; }
public long AvailableCredit { get; set; }
public double Balance { get; set; }
public long? CertainDate { get; set; }
public string CredentialsId { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string UserId { get; set; }
public bool Closed { get; set; }
public List<Transaction> Transactions { get; set; } = new List<Transaction>();
}
public class Transaction
{
public string AccountId { get; set; }
public long Amount { get; set; }
public string CategoryId { get; set; }
public string CredentialsId { get; set; }
public long Date { get; set; }
public string Description { get; set; }
public string FormattedDescription { get; set; }
public string Id { get; set; }
public long Inserted { get; set; }
public long LastModified { get; set; }
public long OriginalAmount { get; set; }
public long OriginalDate { get; set; }
public string OriginalDescription { get; set; }
public long Timestamp { get; set; }
public string Type { get; set; }
public string UserId { get; set; }
}
}
To each Account class in Response class I want to assign a list of transactions by mapping Id from Account class and AccountId from Transaction class. I am aware I could do that by iterating over Accounts or Transactions and checking if both ids are equal and then assign to proper collection but that would cause nested iterations and I am not sure about its efficiency. What should I do? The only contraints in this model are those two lists in the beggining.

How to CreatMap List of Object with appling conditions Automapper

I have Domain object ProductType. It contains parentproducttypeId column. If product type is a subset of another producttype that parentproducttypeId to set to that producttypeId.(ex: T-Shirt is a subset of Casual wears)
public class ProductType : IEntityBase
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int PerentProductTypeId { get; set; }
public int Active { get; set; }
public DateTime UpdatedOnUTC { get; set; }
}
In API view model I have two classes
public class ParentProductTypeModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<ChildProductTypeModel> ChildProductTypeList { get; set; }
public ParentProductTypeModel()
{
ChildProductTypeList = new List<ChildProductTypeModel>();
}
}
public class ChildProductTypeModel
{
public int Id { get; set; }
public string Name { get; set; }
}
I need to Map returned List of Product Type to this ParentProductTypeModel List with setting all subset lists according to there parentproducttypeId. If parentproducttypeId equals to 0 it is parentproduct.

represent a help table in entity framework

Table 1: Articles
Table 2: ArticleCategories
how do I represent the relationship between the two tables which is a 1->1 relationship:
I can do the following, but I'm not sure it's the correct way :
public class Article
{
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public ArticleCategory Category { get; set; }
}
public class ArticleCategory
{
public int CategoryIndex { get; set; }
public string Name { get; set; }
}
By convention, Code First expects an Id property for each class/table. Then you can do something like this:
public class Article
{
public int Id { get; set; }
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public int ArticleCategoryId { get; set; }
public virtual ArticleCategory ArticleCategory { get; set; }
}
public class ArticleCategory
{
public int Id { get; set; }
public int CategoryIndex { get; set; }
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
Note the virtual keyword. EF Code First needs this so it can perform its magic behind the scenes.
Now, if you are working with an Article, you can get all it's category info by doing article.ArticleCategory, and if you have an ArticleCategory you can find out what article it refers to with articleCategory.Articles.Single().
For more info, see this article by Scott Gu:
http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx

Categories

Resources