public async Task <List <WidgetDetailsVm <T> > > Handle(GetWidgetsQuery <T> request, CancellationToken cancellationToken) { var currentUserId = currentUserService.GetCurrentUserId(); var dbWidgets = await(from application in context.Applications join widget in context.Widgets on application.Id equals widget.ApplicationId where request.WidgetsIds.Contains(widget.Id) && application.UserId == currentUserId select widget) .AsNoTracking() .ToListAsync(); var widgetHandlerFactory = new WidgetsHandlerFactory(); var result = new List <WidgetDetailsVm <T> >(); foreach (var dbWidget in dbWidgets) { var widgetHandler = widgetHandlerFactory.Make(dbWidget.Type); var widgetData = new WidgetDetailsVm <T> { WidgetData = (T)widgetHandler.FromJson(dbWidget.Data), Widget = mapper.Map <WidgetDto>(dbWidget) }; result.Add(widgetData); } return(result); }
public async Task <Guid> Handle(CreateWidgetCommand request, CancellationToken cancellationToken) { var currentUserId = currentUserService.GetCurrentUserId(); var application = await context.Applications .FirstOrDefaultAsync(app => app.Id == request.ApplicationId && app.UserId == currentUserId); if (application == null) { throw new EntityNotFoundException(nameof(Application), request.ApplicationId); } var widgetHandlerFactory = new WidgetsHandlerFactory(); var widgetHandler = widgetHandlerFactory.Make(request.WidgetType); var widget = new Widget(request.Name, application.Id) { Type = request.WidgetType.ToString(), Data = widgetHandler.GetDefaultJson() }; await context.Widgets.AddAsync(widget); await context.SaveChangesAsync(); return(widget.Id); }
public async Task <Unit> Handle(UpdateWidgetCommand <T> request, CancellationToken cancellationToken) { var currentUserId = currentUserService.GetCurrentUserId(); var dbWidget = await(from widget in context.Widgets join application in context.Applications on widget.ApplicationId equals application.Id where widget.Id == request.Id && application.UserId == currentUserId select widget).FirstOrDefaultAsync(); if (dbWidget == null) { throw new EntityNotFoundException(nameof(Widget), request.Id); } var widgetHandlerFactory = new WidgetsHandlerFactory(); var widgetHandler = widgetHandlerFactory.Make(dbWidget.Type); dbWidget.Name = request.Name; dbWidget.Data = widgetHandler.GetJson(request.WidgetData); context.Widgets.Update(dbWidget); await context.SaveChangesAsync(); return(Unit.Value); }
public void ShouldMakeWidgetHandlerGivenAllWidgetTypesAsEnum(WidgetType widgetType) { var widgetHandler = factory.Make(widgetType); widgetHandler.Should().NotBeNull(); }