Skip to content

Commit

Permalink
#9 - started on the middleware actions
Browse files Browse the repository at this point in the history
#12 - updated the todo app - may need more work
#8 - started work on the transaction support, conflicts from couch
#10 phasing out .net 4.5, will move to support only standard
  • Loading branch information
dbones committed Apr 13, 2019
1 parent d146a6f commit da026ab
Show file tree
Hide file tree
Showing 60 changed files with 1,307 additions and 233 deletions.
58 changes: 0 additions & 58 deletions README.markdown

This file was deleted.

5 changes: 5 additions & 0 deletions nuspec/changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@



0.11.x
======
.NET 4.5.2 (for versions 0.11.2 +)
fix for Newtonsoft returning an empty string for null's
fix for List<string>
added simple logger (only to be used for development)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public virtual async Task<CollectionResource<TodoResource>> Get([FromQuery] AllQ
{
if (queryString == null) throw new ArgumentNullException(nameof(queryString));

var query = _mapper.Map<Ports.Queries.AllTasks>(queryString);
var query = _mapper.Map<Ports.Queries.AllTodoItems>(queryString);
var tasks = await _mediator.Send(query);

var result = _mapper.Map<CollectionResource<TodoResource>>(tasks);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace Todo.Service.Infrastructure.Behaviours
{
using System.Threading;
using ArmChair;
using MediatR;
using System.Threading.Tasks;

public class UnitOfWorkBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
public class UnitOfWorkBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
private readonly ISession _session;

Expand All @@ -13,13 +14,14 @@ public UnitOfWorkBehavior(ISession session)
_session = session;
}

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var response = await next();

_session.Commit();

return response;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using ArmChair.EntityManagement.Config;

public class TaskClassMap : ClassMap<Models.Task>
public class TaskClassMap : ClassMap<Models.TodoItem>
{
public TaskClassMap()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class QueriesMapping : AutoMapper.Profile
{
public QueriesMapping()
{
CreateMap<AllQueryString, AllTasks>();
CreateMap<AllQueryString, AllTodoItems>();
CreateMap<ByActiveQueryString, TasksByActive>();
CreateMap<ByPriorityQueryString, TasksByPriority>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ public ResourcesMapping(IConfigurationRoot config)
var port = webServerConfig.Port == 80 ? "" : $":{webServerConfig.Port}";
baseUrl = $"{http}{domain}{port}/api/v1/tasks";

CreateMap<Task, TodoResource>()
CreateMap<TodoItem, TodoResource>()
.ForMember(dest => dest.Links, opt => opt.MapFrom(src => GetLinksFromTask(src)))
.ForMember(dest => dest.Actions, opt => opt.MapFrom(src => GetActionsFromTask(src)));

CreateMap<IEnumerable<Task>, CollectionResource<TodoResource>>()
CreateMap<IEnumerable<TodoItem>, CollectionResource<TodoResource>>()
.ForMember(dest => dest.Data, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Links, opt => opt.MapFrom(src => GetCollectionLinksFromTask(src)))
.ForMember(dest => dest.Actions, opt => opt.MapFrom(src => GetCollectionActionsFromTask(src)));

}

protected IDictionary<string, string> GetLinksFromTask(Task task)
protected IDictionary<string, string> GetLinksFromTask(TodoItem todoItem)
{
var result = new Dictionary<string,string>();
result.Add("collection", $"{baseUrl}/");
return result;
}


protected IDictionary<string, string> GetActionsFromTask(Task task)
protected IDictionary<string, string> GetActionsFromTask(TodoItem todoItem)
{
var result = new Dictionary<string,string>();
if (!task.IsComplete) result.Add("update", $"{baseUrl}/{task.Id}");
if (!task.IsComplete) result.Add("complete", $"{baseUrl}/{task.Id}/complete");
result.Add("remove", $"{baseUrl}/{task.Id}");
if (!todoItem.IsComplete) result.Add("update", $"{baseUrl}/{todoItem.Id}");
if (!todoItem.IsComplete) result.Add("complete", $"{baseUrl}/{todoItem.Id}/complete");
result.Add("remove", $"{baseUrl}/{todoItem.Id}");
return result;
}


protected IDictionary<string, string> GetCollectionLinksFromTask(IEnumerable<Task> task)
protected IDictionary<string, string> GetCollectionLinksFromTask(IEnumerable<TodoItem> task)
{
var result = new Dictionary<string,string>();
result.Add("self", $"{baseUrl}/");
Expand All @@ -58,7 +58,7 @@ protected IDictionary<string, string> GetCollectionLinksFromTask(IEnumerable<Tas
}


protected IDictionary<string, string> GetCollectionActionsFromTask(IEnumerable<Task> task)
protected IDictionary<string, string> GetCollectionActionsFromTask(IEnumerable<TodoItem> task)
{
var result = new Dictionary<string,string>();
result.Add("createTodoTask", $"{baseUrl}/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected void EnsureDatabase(Connection conn, DatabaseConfig dbConfig)
/// <param name="database">the database instance</param>
protected void SetupDatabase(Database database)
{
database.Settings.QueryPipeline.SetItemIterator(new ListsItemIterator<QueryContext>());
//database.Settings.QueryPipeline.SetItemIterator(new ListsItemIterator<QueryContext>());

//register maps
database.Register(new[] { new TaskClassMap() });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
{
using System;

public class Task : EntityRoot
public class TodoItem : EntityRoot
{
private string _description;
private bool _isComplete;

protected Task()
protected TodoItem()
{

}

public Task(string description, PriorityLevel priorityLevel)
public TodoItem(string description, PriorityLevel priorityLevel)
{
Created = DateTime.UtcNow;
_description = description;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
namespace Todo.Service.Ports.Commands
{
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using ArmChair;
using MediatR;
using Models;

public class CreateUpdateTask : IRequest<Task>
public class CreateUpdateTask : IRequest<TodoItem>
{
[Required]
public string Description { get; set; }
public PriorityLevel PriorityLevel { get; set; } = PriorityLevel.Medium;
}

public class CreateUpdateTaskHandler : IRequestHandler<CreateUpdateTask, Task>
public class CreateUpdateTaskHandler : IRequestHandler<CreateUpdateTask, TodoItem>
{
private readonly ISession _session;

Expand All @@ -21,12 +23,14 @@ public CreateUpdateTaskHandler(ISession session)
_session = session;
}

public Task Handle(CreateUpdateTask message)


public Task<TodoItem> Handle(CreateUpdateTask request, CancellationToken cancellationToken)
{
var task = new Task(message.Description, message.PriorityLevel);
_session.Add(task);
var todoItem = new TodoItem(request.Description, request.PriorityLevel);
_session.Add(todoItem);

return task;
return Task.FromResult(todoItem);
}
}
}
39 changes: 0 additions & 39 deletions samples/todo/Todo.Service/Ports/Commands/MarkTaskAsComplete.cs

This file was deleted.

42 changes: 42 additions & 0 deletions samples/todo/Todo.Service/Ports/Commands/MarkTodoItemAsComplete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Todo.Service.Ports.Commands
{
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using ArmChair;
using Infrastructure.Exceptions;
using MediatR;
using Models;

public class MarkTaskAsComplete : IRequest<TodoItem>
{
[Required]
public string Id { get; set; }
}

public class MarkTaskAsCompleteHandler : IRequestHandler<MarkTaskAsComplete, TodoItem>
{
private readonly ISession _session;

public MarkTaskAsCompleteHandler(ISession session)
{
_session = session;
}

public Task<TodoItem> Handle(MarkTaskAsComplete request, CancellationToken cancellationToken)
{
if (request == null) throw new ArgumentNullException(nameof(request));
var todoItem = _session.GetById<TodoItem>(request.Id);

if (todoItem == null)
{
throw new NotFoundException("Task", request.Id);
}

todoItem.IsComplete = true;
return new Task<TodoItem>(() => todoItem);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Todo.Service.Ports.Commands
{
using System;
using System.Threading;
using System.Threading.Tasks;
using ArmChair;
using MediatR;

Expand All @@ -19,16 +21,20 @@ public PruneStaleTasksHandler(IMediator mediator, ISession session)
_session = session;
}

public void Handle(PuneStaleTasks message)
public Task<Unit> Handle(PuneStaleTasks request, CancellationToken cancellationToken)
{
if (message == null) throw new ArgumentNullException(nameof(message));
if (request == null) throw new ArgumentNullException(nameof(request));

var tasks = _mediator.Send(new Queries.TasksByStale()).Result;
var todoItems = _mediator.Send(new Queries.TasksByStale()).Result;

if (tasks != null)
if (todoItems != null)
{
_session.RemoveRange(tasks);
_session.RemoveRange(todoItems);
}

return Task.FromResult(new Unit());
}


}
}
Loading

0 comments on commit da026ab

Please sign in to comment.