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

Fix/guid return command #37

Merged
merged 3 commits into from
Jul 4, 2024
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
3 changes: 2 additions & 1 deletion Samples/eCommerce/Basic/API/Carts/Cart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public class Cart(IGrainFactory grainFactory, ICartQueries cartQueries) : Contro
/// <param name="addItemToCart">Payload holding the command.</param>
/// <returns>Awaitable task.</returns>
[HttpPost("add-item")]
public async Task AddItem([FromBody] AddItemToCart addItemToCart)
public async Task<Guid> AddItem([FromBody] AddItemToCart addItemToCart)
{
var cartId = (CartId)(User.Identity?.GetUserIdAsGuid() ?? Guid.Empty);
var price = await grainFactory.GetGrain<IProductPrice>(addItemToCart.Sku).GetPrice();
await grainFactory.GetGrain<ICart>(cartId).AddItem(addItemToCart.Sku, price, addItemToCart.Quantity);
return cartId;
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions Samples/eCommerce/Basic/Web/API/Carts/AddItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Command, CommandValidator, CommandPropertyValidators } from '@cratis/applications/commands';
import { useCommand, SetCommandValues, ClearCommandValues } from '@cratis/applications.react/commands';
import { Validator } from '@cratis/applications/validation';
import { Guid } from '@cratis/fundamentals';
import Handlebars from 'handlebars';

const routeTemplate = Handlebars.compile('/api/carts/add-item');
Expand All @@ -22,7 +23,7 @@ export class AddItemValidator extends CommandValidator {
};
}

export class AddItem extends Command<IAddItem> implements IAddItem {
export class AddItem extends Command<IAddItem, Guid> implements IAddItem {
readonly route: string = '/api/carts/add-item';
readonly routeTemplate: Handlebars.TemplateDelegate = routeTemplate;
readonly validation: CommandValidator = new AddItemValidator();
Expand All @@ -31,7 +32,7 @@ export class AddItem extends Command<IAddItem> implements IAddItem {
private _quantity!: number;

constructor() {
super(Object, false);
super(Guid, false);
}

get requestArguments(): string[] {
Expand Down
7 changes: 5 additions & 2 deletions Source/DotNET/Tools/ProxyGenerator/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ public static CommandDescriptor ToCommandDescriptor(this MethodInfo method, stri

var propertiesWithComplexTypes = properties.Where(_ => !_.OriginalType.IsKnownType());
typesInvolved.AddRange(propertiesWithComplexTypes.Select(_ => _.OriginalType));
var imports = typesInvolved.GetImports(targetPath, method.DeclaringType!.ResolveTargetPath(segmentsToSkip), segmentsToSkip);

var imports = typesInvolved.GetImports(targetPath, method.DeclaringType!.ResolveTargetPath(segmentsToSkip), segmentsToSkip).ToList();
if (responseModel.Type is not null && responseModel.Type.GetTargetType().TryGetImportStatement(out var responseTypeImportStatement))
{
imports.Add(responseTypeImportStatement);
}
var additionalTypesInvolved = new List<Type>();
foreach (var property in propertiesWithComplexTypes)
{
Expand Down
30 changes: 30 additions & 0 deletions Source/DotNET/Tools/ProxyGenerator/TargetTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using Cratis.Applications.ProxyGenerator.Templates;

namespace Cratis.Applications.ProxyGenerator;

/// <summary>
/// Extension methods for <see cref="TargetType"/>.
/// </summary>
public static class TargetTypeExtensions
{
/// <summary>
/// Try to get import statement from target type.
/// </summary>
/// <param name="targetType">The target type.</param>
/// <param name="importStatement">The resulting import statement if it requires so.</param>
/// <returns>True if it needs an import statement, false if not.</returns>
public static bool TryGetImportStatement(this TargetType targetType, [NotNullWhen(true)]out ImportStatement? importStatement)
{
importStatement = null;
var requiresImport = !string.IsNullOrEmpty(targetType.Module);
if (requiresImport)
{
importStatement = new ImportStatement(targetType.Type, targetType.Module);
}
return requiresImport;
}
}
14 changes: 9 additions & 5 deletions Source/DotNET/Tools/ProxyGenerator/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,15 @@ public static string ResolveTargetPath(this Type type, int segmentsToSkip)
public static IEnumerable<ImportStatement> GetImports(this IEnumerable<Type> types, string targetPath, string relativePath, int segmentsToSkip) =>
types.Select(_ =>
{
var fullPath = Path.Join(targetPath, relativePath);
var fullPathForType = Path.Join(targetPath, _.ResolveTargetPath(segmentsToSkip));
var importPath = Path.GetRelativePath(fullPath, fullPathForType);
importPath = $"{importPath}/{_.Name}";
return new ImportStatement(_.GetTargetType().Type, importPath);
var targetType = _.GetTargetType();
var importPath = targetType.Module;
if (string.IsNullOrEmpty(importPath))
{
var fullPath = Path.Join(targetPath, relativePath);
var fullPathForType = Path.Join(targetPath, _.ResolveTargetPath(segmentsToSkip));
importPath = $"{Path.GetRelativePath(fullPath, fullPathForType)}/{_.Name}";
}
return new ImportStatement(targetType.Type, importPath);
}).ToArray();

/// <summary>
Expand Down
Loading