Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check collection for bought client #15

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Security.Claims;
using account_service.CustomException;
using account_service.DTO.Current;
using account_service.DTO.Painting;
using account_service.Service.CollectionService;
using account_service.Service.CurrentLoggedInService;
using account_service.ValueObjects;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace account_service.Controllers.CollectionController;

[Route("api/v1/account-service")]
[ApiController]
public class CollectionController: ControllerBase
{
private readonly ICollectionService _collectionService;
private readonly IMapper _mapper;

public CollectionController(ICollectionService collectionService , IMapper mapper)
{
_collectionService = collectionService;
_mapper = mapper;
}

[HttpGet]
[Route("current/paintings")]
[Authorize]
public ActionResult GetCurrentLoggedInUserPaintingCollection()
{
try
{
var auth0UserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var currentLoggedInUserPainting = _collectionService.GetCurrentLoggedInUserPaintingCollection(auth0UserId);
var currentLoggedInUserPaintingDto = _mapper.Map<IEnumerable<ReadPaintingDto>>(currentLoggedInUserPainting);
return Ok(currentLoggedInUserPaintingDto);
}catch (UserNotFoundException e)
{
return NotFound(e.message);
}
catch (Exception e) {
Console.WriteLine(e);
return Problem(e.GetBaseException().ToString());
}
}

[HttpGet]
[Route("collection/paintings/{userId}")]
[Authorize]
public ActionResult GetPaintingCollectionByUserId(Guid userId)
{
try
{
var currentLoggedInUserPainting = _collectionService.GetPaintingCollectionByUserId(userId);
var currentLoggedInUserPaintingDto = _mapper.Map<IEnumerable<ReadPaintingDto>>(currentLoggedInUserPainting);
return Ok(currentLoggedInUserPaintingDto);
}catch (UserNotFoundException e)
{
return NotFound(e.message);
}
catch (Exception e) {
Console.WriteLine(e);
return Problem(e.GetBaseException().ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using account_service.Models;

namespace account_service.Repository.CollectionRepo;

public class CollectionRepo: ICollectionRepo
{
private readonly ArtistBlockDbContext _context;

public CollectionRepo(ArtistBlockDbContext context)
{
_context = context;
}

public IEnumerable<Painting> GetCurrentLoggedInUserPaintingCollection(Guid currentLoggedInUserId)
{
var currentLoggedInUserPaintings =
_context.Paintings.Where(painting => painting.RegisteredUserId.Equals(currentLoggedInUserId));
return currentLoggedInUserPaintings;
}

public IEnumerable<Painting> GetPaintingCollectionByUserId(Guid userId)
{
var currentLoggedInUserPaintings =
_context.Paintings.Where(painting => painting.RegisteredUserId.Equals(userId));
return currentLoggedInUserPaintings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using account_service.Models;

namespace account_service.Repository.CollectionRepo;

public interface ICollectionRepo
{
IEnumerable<Painting> GetCurrentLoggedInUserPaintingCollection(Guid currentLoggedInUserId);
IEnumerable<Painting> GetPaintingCollectionByUserId(Guid userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using account_service.Models;
using account_service.Repository.CollectionRepo;
using account_service.Repository.RegistrationRepo;

namespace account_service.Service.CollectionService;

public class CollectionService: ICollectionService
{
private readonly ICollectionRepo _collectionRepo;
private readonly IRegistrationRepo _registrationRepo;

public CollectionService(ICollectionRepo collectionRepo, IRegistrationRepo registrationRepo)
{
_collectionRepo = collectionRepo;
_registrationRepo = registrationRepo;
}


public IEnumerable<Painting> GetCurrentLoggedInUserPaintingCollection(string auth0UserId)
{
var currentLoggedInUserId = _registrationRepo.GetUserInfromation(auth0UserId).RegisteredUserId;
return _collectionRepo.GetCurrentLoggedInUserPaintingCollection(currentLoggedInUserId);
}

public IEnumerable<Painting> GetPaintingCollectionByUserId(Guid userId)
{
return _collectionRepo.GetPaintingCollectionByUserId(userId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using account_service.Models;
using account_service.ValueObjects;

namespace account_service.Service.CollectionService;

public interface ICollectionService
{
IEnumerable<Painting> GetCurrentLoggedInUserPaintingCollection(string auth0UserId);
IEnumerable<Painting> GetPaintingCollectionByUserId(Guid userId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System.Security.Claims;
using System.Text.Json.Serialization;
using account_service.Repository;
using account_service.Repository.CollectionRepo;
using account_service.Repository.GanRepo;
using account_service.Repository.BuyRepo;

using account_service.Repository.PaintingRepo;
using account_service.Repository.RegistrationRepo;
using account_service.Repository.SearchRepo;
using account_service.Repository.SpecialityRepo;
using account_service.Service.CollectionService;
using account_service.Service.BuyController;
using account_service.Service.CurrentLoggedInService;
using account_service.Service.GanService;
Expand Down Expand Up @@ -51,6 +53,9 @@ public void ConfigureServices(IServiceCollection services) {
services.AddScoped<ISpecialityRepo, SpecialityRepo>();
services.AddScoped<ISpecialityService, SpecialityService>();

services.AddScoped<ICollectionRepo, CollectionRepo>();
services.AddScoped<ICollectionService, CollectionService>();

services.AddScoped<IBuyService, BuyService>();
services.AddScoped<IBuyRepo, BuyRepo>();

Expand Down