Skip to content

Commit

Permalink
VM-868: Vendor details (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvishnyakov authored Jan 31, 2023
1 parent 8e326f5 commit 0990fe6
Show file tree
Hide file tree
Showing 31 changed files with 491 additions and 365 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using VirtoCommerce.CustomerModule.Core.Model;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact;
Expand All @@ -7,17 +8,27 @@ namespace VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates
{
public class MemberAggregateFactory : IMemberAggregateFactory
{
public virtual T Create<T>(Member member) where T : class, IMemberAggregateRoot
public virtual T Create<T>(Member member)
where T : class, IMemberAggregateRoot
{
var result = default(T);

if (member != null)
{
result = member.MemberType switch
// This is workaround for addresses & dynamic properties commands
if (typeof(T).Name == nameof(MemberAggregateRootBase))
{
nameof(Organization) => (T)(object)AbstractTypeFactory<OrganizationAggregate>.TryCreateInstance(),
_ => (T)(object)AbstractTypeFactory<ContactAggregate>.TryCreateInstance()
};
result = member.MemberType switch
{
nameof(CustomerModule.Core.Model.Organization) => (T)(object)AbstractTypeFactory<OrganizationAggregate>.TryCreateInstance(),
nameof(CustomerModule.Core.Model.Contact) => (T)(object)AbstractTypeFactory<ContactAggregate>.TryCreateInstance(),
_ => throw new ArgumentOutOfRangeException(nameof(member), "Member type isn't supported")
};
}
else
{
result = AbstractTypeFactory<T>.TryCreateInstance();
}

result.Member = member;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Vendor;

public interface IVendorAggregateRepository: IMemberAggregateRootRepository
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Vendor;

public class VendorAggregate: MemberAggregateRootBase
{
public CustomerModule.Core.Model.Contact Contact => Member as CustomerModule.Core.Model.Contact;

public CustomerModule.Core.Model.Organization Organization => Member as CustomerModule.Core.Model.Organization;

public CustomerModule.Core.Model.Vendor Vendor => Member as CustomerModule.Core.Model.Vendor;

public IEnumerable<ExpVendorRating> Ratings { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using VirtoCommerce.CustomerModule.Core.Services;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Vendor;

public class VendorAggregateRepository: MemberAggregateRootRepository, IVendorAggregateRepository
{
public VendorAggregateRepository(IMemberService memberService, IMemberAggregateFactory factory) : base(memberService, factory)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using VirtoCommerce.Platform.Core.Security;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Organization;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Vendor;
using VirtoCommerce.ProfileExperienceApiModule.Data.Commands;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Authorization
Expand Down Expand Up @@ -65,6 +66,10 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
{
result = currentContact.Organizations.Contains(organizationAggregate.Organization.Id);
}
else if (context.Resource is VendorAggregate)
{
result = true;
}

else if (context.Resource is Role role)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using VirtoCommerce.ExperienceApiModule.Core;

namespace VirtoCommerce.ProfileExperienceApiModule.Data;

public class ExpVendorRating: ExpRating
{
public string StoreId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Types;
using MediatR;
using VirtoCommerce.ExperienceApiModule.Core.Extensions;
using VirtoCommerce.ExperienceApiModule.Core.Helpers;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates;
using VirtoCommerce.ProfileExperienceApiModule.Data.Queries;
using VirtoCommerce.ProfileExperienceApiModule.Data.Schemas;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Extensions;

public static class SchemaExtensions
{
public static void AddMemberQuery<TAggregate, TType, TQuery>(this ISchema schema, IMediator mediator, string name, Func<string, object, Task> checkAuthAsync)
where TAggregate: MemberAggregateRootBase
where TType: MemberBaseType<TAggregate>
where TQuery: GetMemberByIdQueryBase<TAggregate>, new()
{
schema.Query.AddField(new FieldType
{
Name = name,
Arguments = new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>> { Name = "id" },
new QueryArgument<StringGraphType> { Name = "userId" }
),
Type = GraphTypeExtenstionHelper.GetActualType<TType>(),
Resolver = new AsyncFieldResolver<object>(async context =>
{
var query = new TQuery { Id = context.GetArgument<string>("id") };
var aggregate = await mediator.Send(query);

await checkAuthAsync(context.GetCurrentUserId(), aggregate);

//store aggregate in the user context for future usage in the graph types resolvers
context.UserContext.Add($"{name}Aggregate", aggregate);

return aggregate;
})
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public class GetContactByIdQuery : GetMemberByIdQueryBase<ContactAggregate>
{
public GetContactByIdQuery()
{
}

[Obsolete("Use parameterless constructor with object initialization")]
public GetContactByIdQuery(string contactId)
{
ContactId = contactId;
}

[Obsolete("Use Id instead")]
public string ContactId
{
get => Id;
set => Id = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Contact;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public class GetContactByIdQueryHandler : GetMemberByIdQueryHandlerBase<GetContactByIdQuery, ContactAggregate>
{
public GetContactByIdQueryHandler(IContactAggregateRepository contactAggregateRepository): base(contactAggregateRepository)
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using VirtoCommerce.ExperienceApiModule.Core.Infrastructure;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public abstract class GetMemberByIdQueryBase<TAggregate> : IQuery<TAggregate>
where TAggregate : MemberAggregateRootBase
{
public string Id { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading.Tasks;
using System.Threading;
using VirtoCommerce.ExperienceApiModule.Core.Infrastructure;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public abstract class GetMemberByIdQueryHandlerBase<TQuery, TAggregate> : IQueryHandler<TQuery, TAggregate>
where TQuery: GetMemberByIdQueryBase<TAggregate>
where TAggregate: MemberAggregateRootBase
{
private readonly IMemberAggregateRootRepository _aggregateRepository;

protected GetMemberByIdQueryHandlerBase(IMemberAggregateRootRepository aggregateRepository)
{
_aggregateRepository = aggregateRepository;
}

public virtual Task<TAggregate> Handle(TQuery request, CancellationToken cancellationToken)
{
return _aggregateRepository.GetMemberAggregateRootByIdAsync<TAggregate>(request.Id);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Organization;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public class GetOrganizationByIdQuery : GetMemberByIdQueryBase<OrganizationAggregate>
{
public GetOrganizationByIdQuery()
{
}

[Obsolete("Use parameterless constructor with object initialization")]
public GetOrganizationByIdQuery(string organizationId)
{
OrganizationId = organizationId;
}

[Obsolete("Use Id instead")]
public string OrganizationId
{
get => Id;
set => Id = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Organization;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public class GetOrganizationByIdQueryHandler : GetMemberByIdQueryHandlerBase<GetOrganizationByIdQuery, OrganizationAggregate>
{
public GetOrganizationByIdQueryHandler(IOrganizationAggregateRepository contactAggregateRepository) : base(contactAggregateRepository)
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Vendor;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public class GetVendorByIdQuery: GetMemberByIdQueryBase<VendorAggregate>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Threading;
using System.Threading.Tasks;
using VirtoCommerce.ExperienceApiModule.Core.Pipelines;
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates.Vendor;

namespace VirtoCommerce.ProfileExperienceApiModule.Data.Queries;

public class GetVendorByIdQueryHandler: GetMemberByIdQueryHandlerBase<GetVendorByIdQuery, VendorAggregate>
{
private readonly IGenericPipelineLauncher _pipeline;

public GetVendorByIdQueryHandler(IVendorAggregateRepository aggregateRepository, IGenericPipelineLauncher pipeline) : base(aggregateRepository)
{
_pipeline = pipeline;
}

public override async Task<VendorAggregate> Handle(GetVendorByIdQuery request, CancellationToken cancellationToken)
{
var result = await base.Handle(request, cancellationToken);
await _pipeline.Execute(result);
return result;
}
}
Loading

0 comments on commit 0990fe6

Please sign in to comment.