Skip to content

Commit

Permalink
Implemented Cart api
Browse files Browse the repository at this point in the history
  • Loading branch information
Olukeye committed Mar 7, 2024
1 parent 303650a commit b264801
Show file tree
Hide file tree
Showing 31 changed files with 904 additions and 6 deletions.
19 changes: 19 additions & 0 deletions JendStore.Cart.Service.API/Configurations/MapperInitilizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using AutoMapper;
using JendStore.Cart.Service.API.DTO;
using JendStore.Cart.Service.API.Models;

namespace JendStore.Cart.Service.API.Configurations
{
public class MapperInitilizer
{
public static MapperConfiguration RegisterMaps()
{
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<CartDetail, CartDetailDto>().ReverseMap();
cfg.CreateMap<CartHeader, CartHeaderDto>().ReverseMap();
});
return config;
}

}
}
66 changes: 66 additions & 0 deletions JendStore.Cart.Service.API/Controllers/CartController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using AutoMapper;
using JendStore.Cart.Service.API.DTO;
using JendStore.Cart.Service.API.Models;
using JendStore.Cart.Service.API.Repository.Interface;
using Microsoft.AspNetCore.Mvc;

namespace JendStore.Cart.Service.API.Controllers
{
[Route("api/cart")]
[ApiController]
public class CartController : ControllerBase
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly ILogger<CartController> _logger;
protected ResponseDTOStatus _response;

public CartController(IUnitOfWork unitOfWork, IMapper mapper1, ILogger<CartController> logger)
{
_unitOfWork = unitOfWork;
_mapper = mapper1;
_logger = logger;
_response = new();
}

[HttpPost("CartUpsert")]
public async Task<IActionResult> CartUspsert(CartDto cartDto)
{
var cartHeder = await _unitOfWork.CartHeaders.Get(u => u.UserId == cartDto.CartHeader.UserId);
if (cartHeder == null)
{
//Create header and detail
CartHeader cartHeader = _mapper.Map<CartHeader>(cartDto.CartHeader);
await _unitOfWork.CartHeaders.Insert(cartHeader);
await _unitOfWork.Save();
cartDto.CartDetails.First().CartHeaderId = cartHeader.CartHeaderId;
await _unitOfWork.CartDetails.Insert(_mapper.Map<CartDetail>(cartDto.CartDetails.First()));
await _unitOfWork.Save();
}
else
{
//if header is not null
//check if detail has same product
var cartDetail = await _unitOfWork.CartDetails.Get(u => u.ProductId == cartDto.CartDetails.First().ProductId && u.CartHeaderId == cartHeder.CartHeaderId);
if (cartDetail == null)
{
//create cartDeatil
cartDto.CartDetails.First().CartHeaderId = cartHeder.CartHeaderId;
await _unitOfWork.CartDetails.Insert(_mapper.Map<CartDetail>(cartDto.CartDetails.First()));
await _unitOfWork.Save();
}
else
{
//update quantity in cart detail
cartDto.CartDetails.First().Quantity += cartDetail.Quantity;
cartDto.CartDetails.First().CartHeaderId = cartDetail.CartHeaderId;
cartDto.CartDetails.First().CartDetailId = cartDetail.CartDetailId;
_unitOfWork.CartDetails.Update(_mapper.Map<CartDetail>(cartDto.CartDetails.First()));
await _unitOfWork.Save();
}
}
_response.Result = cartDto;
return Ok(_response);
}
}
}
13 changes: 13 additions & 0 deletions JendStore.Cart.Service.API/DTO/CartDetailDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

namespace JendStore.Cart.Service.API.DTO
{
public class CartDetailDto
{
public int CartDetailId { get; set; }
public int CartHeaderId { get; set; }
public CartHeaderDto? CartHeader { get; set; }
public int ProductId { get; set; }
public ProductDto? Product { get; set; }
public int Quantity { get; set; }
}
}
8 changes: 8 additions & 0 deletions JendStore.Cart.Service.API/DTO/CartDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace JendStore.Cart.Service.API.DTO
{
public class CartDto
{
public CartHeaderDto CartHeader { get; set; }
public IEnumerable<CartDetailDto>? CartDetails { get; set; }
}
}
11 changes: 11 additions & 0 deletions JendStore.Cart.Service.API/DTO/CartHeaderDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace JendStore.Cart.Service.API.DTO
{
public class CartHeaderDto
{
public int CartHeaderId { get; set; }
public string? UserId { get; set; }
public string? Code { get; set; }
public double Discount { get; set; }
public double Total { get; set; }
}
}
22 changes: 22 additions & 0 deletions JendStore.Cart.Service.API/DTO/CouponDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace JendStore.Cart.Service.API.DTO
{

public class CreateCouponDTO
{
public string Code { get; set; }
public double Discount { get; set; }
public int MinAmount { get; set; }
}

public class UpdateCouponDTO: CreateCouponDTO
{

}

public class CouponDTO : CreateCouponDTO
{
public int CouponId { get; set; }
}
}
19 changes: 19 additions & 0 deletions JendStore.Cart.Service.API/DTO/ProductDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;

namespace JendStore.Cart.Service.API.DTO
{
public class ProductDto
{
public int ProductId { get; set; }

public string Name { get; set; }

public double Price { get; set; }

public string Description { get; set; }

public string CategoryName { get; set; }

public string ImageUrl { get; set; }
}
}
12 changes: 12 additions & 0 deletions JendStore.Cart.Service.API/DTO/ResponseDTOStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace JendStore.Cart.Service.API.DTO
{
public class ResponseDTOStatus
{
public object? Result { get; set; }
public bool IsSuccess { get; set; } = true;
public string Message { get; set; } = string.Empty;
public int StatusCode { get; set; }

}
}

23 changes: 23 additions & 0 deletions JendStore.Cart.Service.API/Data/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using JendStore.Cart.Service.API.Models;
using Microsoft.EntityFrameworkCore;

namespace JendStore.Cart.Service.API.Data
{
public class DatabaseContext: DbContext
{

public DatabaseContext(DbContextOptions options) : base(options)
{

}

public DbSet<CartDetail> CartDetails { get; set; }
public DbSet<CartHeader> CartHeaders { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace JendStore.Cart.Service.API.ResponseHandler
{
public class ResponseStatus
{
public string? Message { get; set; } = string.Empty;
public int Status { get; set; }
public int StatusCode { get; set; }

public override string ToString() => JsonConvert.SerializeObject(this);
}
}
26 changes: 26 additions & 0 deletions JendStore.Cart.Service.API/JendStore.Cart.Service.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="IdentityFramework.Core" Version="2.3.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.14" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.14" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions JendStore.Cart.Service.API/Migrations/20240304072330_CartTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JendStore.Cart.Service.API.Migrations
{
/// <inheritdoc />
public partial class CartTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "CartHeaders",
columns: table => new
{
CartHeaderId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
UserId = table.Column<string>(type: "nvarchar(max)", nullable: true),
Code = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_CartHeaders", x => x.CartHeaderId);
});

migrationBuilder.CreateTable(
name: "CartDetails",
columns: table => new
{
CartDetailId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CartHeaderId = table.Column<int>(type: "int", nullable: false),
ProductId = table.Column<int>(type: "int", nullable: false),
Quantity = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CartDetails", x => x.CartDetailId);
table.ForeignKey(
name: "FK_CartDetails_CartHeaders_CartHeaderId",
column: x => x.CartHeaderId,
principalTable: "CartHeaders",
principalColumn: "CartHeaderId",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_CartDetails_CartHeaderId",
table: "CartDetails",
column: "CartHeaderId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CartDetails");

migrationBuilder.DropTable(
name: "CartHeaders");
}
}
}
Loading

0 comments on commit b264801

Please sign in to comment.