Skip to content

Commit 3ebd820

Browse files
authored
Merge pull request #15 from es-hackathon/feature/cardapi
Feature/cardapi
2 parents bda43e0 + dd4e871 commit 3ebd820

File tree

54 files changed

+941
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+941
-95
lines changed

docs/rest.png

5.42 KB
Loading

src/Bookmark.Application/Bookmark.Application.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,4 @@
2323
<ProjectReference Include="..\Bookmark.Domain\Bookmark.Domain.csproj" />
2424
</ItemGroup>
2525

26-
<ItemGroup>
27-
<Folder Include="Features\" />
28-
</ItemGroup>
29-
3026
</Project>

src/Bookmark.Application/Common/Exceptions/ValidationException.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public ValidationException()
1212
{
1313
Failures = new Dictionary<string, string[]>();
1414
}
15+
public ValidationException(string message) : base(message)
16+
{
17+
}
1518

1619
public ValidationException(List<ValidationFailure> failures)
1720
: this()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using AutoMapper;
2+
3+
namespace Bookmark.Application.Common.Mapping
4+
{
5+
public interface IMapFrom<T>
6+
{
7+
void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType());
8+
}
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using AutoMapper;
2+
using System;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Bookmark.Application.Common.Mapping
7+
{
8+
public class MappingProfile : Profile
9+
{
10+
public MappingProfile()
11+
{
12+
ApplyMappingsFromAssembly(Assembly.GetExecutingAssembly());
13+
}
14+
15+
private void ApplyMappingsFromAssembly(Assembly assembly)
16+
{
17+
var types = assembly.GetExportedTypes()
18+
.Where(t => t.GetInterfaces().Any(i =>
19+
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)))
20+
.ToList();
21+
22+
foreach (var type in types)
23+
{
24+
var instance = Activator.CreateInstance(type);
25+
var methodInfo = type.GetMethod("Mapping");
26+
methodInfo?.Invoke(instance, new object[] { this });
27+
}
28+
}
29+
}
30+
}

src/Bookmark.Application/Common/Middleware/CustomExceptionHandlerMiddleware.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
using Microsoft.Extensions.Logging;
44
using Newtonsoft.Json;
55
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
86
using System.Net;
9-
using System.Text;
107
using System.Threading.Tasks;
118

129
namespace Bookmark.Application.Common.Middleware
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using MediatR;
2+
using System;
3+
4+
namespace Bookmark.Application.Features.CardFeature.Commands.CreateCard
5+
{
6+
public class CreateCardCommand : IRequest<CreateCardViewModel>
7+
{
8+
public Guid Id { get; set; }
9+
public string Name { get; set; }
10+
public string Description { get; set; }
11+
public string Url { get; set; }
12+
public string DisplayIcon { get; set; }
13+
public DateTime? ExpireDate { get; set; }
14+
}
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using AutoMapper;
2+
using Bookmark.Application.Common.Interface;
3+
using Bookmark.Domain.Entities;
4+
using MediatR;
5+
using System;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Bookmark.Application.Features.CardFeature.Commands.CreateCard
10+
{
11+
public class CreateCardCommandHandler : IRequestHandler<CreateCardCommand, CreateCardViewModel>
12+
{
13+
private readonly IApplicationDbContext _context;
14+
private readonly IMapper _mapper;
15+
public CreateCardCommandHandler(IApplicationDbContext context, IMapper mapper)
16+
{
17+
_context = context;
18+
_mapper = mapper;
19+
}
20+
public async Task<CreateCardViewModel> Handle(CreateCardCommand request, CancellationToken cancellationToken)
21+
{
22+
var entity = new Cards
23+
{
24+
Id = Guid.NewGuid(),
25+
Name = request.Name,
26+
Description = request.Description,
27+
DisplayIcon = request.DisplayIcon,
28+
Url = request.Url,
29+
ExpireDate = request.ExpireDate
30+
};
31+
32+
_context.Cards.Add(entity);
33+
34+
await _context.SaveChangesAsync(cancellationToken);
35+
36+
return _mapper.Map<CreateCardViewModel>(entity);
37+
}
38+
39+
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using FluentValidation;
2+
3+
namespace Bookmark.Application.Features.CardFeature.Commands.CreateCard
4+
{
5+
public class CreateCardCommandValidator : AbstractValidator<CreateCardCommand>
6+
{
7+
public CreateCardCommandValidator()
8+
{
9+
RuleFor(x => x.Name).NotEmpty();
10+
RuleFor(x => x.Description).NotEmpty();
11+
RuleFor(x => x.Url).NotEmpty();
12+
}
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using AutoMapper;
2+
using Bookmark.Application.Common.Mapping;
3+
using Bookmark.Domain.Entities;
4+
using System;
5+
6+
namespace Bookmark.Application.Features.CardFeature.Commands.CreateCard
7+
{
8+
public class CreateCardViewModel : IMapFrom<Cards>
9+
{
10+
public Guid Id { get; set; }
11+
public string Name { get; set; }
12+
public string Description { get; set; }
13+
public string Url { get; set; }
14+
public string DisplayIcon { get; set; }
15+
public DateTime? ExpireDate { get; set; }
16+
17+
public void Mapping(Profile profile)
18+
{
19+
profile.CreateMap<Cards, CreateCardViewModel>()
20+
.ForMember(d => d.Id, opt => opt.MapFrom(s => s.Id));
21+
}
22+
23+
}
24+
}

0 commit comments

Comments
 (0)