public static void CanChangePlan(ChangePlan command, AppPlan plan, IAppPlansProvider appPlans) { Guard.NotNull(command, nameof(command)); Validate.It(() => "Cannot change plan.", error => { if (string.IsNullOrWhiteSpace(command.PlanId)) { error(new ValidationError("PlanId is not defined.", nameof(command.PlanId))); } else if (appPlans.GetPlan(command.PlanId) == null) { error(new ValidationError("Plan id not available.", nameof(command.PlanId))); } if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor)) { error(new ValidationError("Plan can only be changed from current user.")); } if (string.Equals(command.PlanId, plan?.PlanId, StringComparison.OrdinalIgnoreCase)) { error(new ValidationError("App has already this plan.")); } }); }
public static void CanChangePlan(ChangePlan command, AppPlan plan, IAppPlansProvider appPlans) { Guard.NotNull(command, nameof(command)); Validate.It(() => "Cannot change plan.", e => { if (string.IsNullOrWhiteSpace(command.PlanId)) { e(Not.Defined("Plan id"), nameof(command.PlanId)); return; } if (appPlans.GetPlan(command.PlanId) == null) { e("A plan with this id does not exist.", nameof(command.PlanId)); } if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor)) { e("Plan can only changed from the user who configured the plan initially."); } if (string.Equals(command.PlanId, plan?.PlanId, StringComparison.OrdinalIgnoreCase)) { e("App has already this plan."); } }); }
public void Should_serialize_and_deserialize() { var plan = new AppPlan(new RefToken("user", "Me"), "free"); var serialized = plan.SerializeAndDeserialize(); serialized.Should().BeEquivalentTo(plan); }
public void Should_serialize_and_deserialize() { var sut = new AppPlan(new RefToken("user", "Me"), "free"); var serialized = JToken.FromObject(sut, serializer).ToObject <AppPlan>(serializer); serialized.ShouldBeEquivalentTo(sut); }
public void Should_serialize_and_deserialize() { var plan = new AppPlan(RefToken.Client("Me"), "free"); var serialized = plan.SerializeAndDeserialize(); Assert.Equal(plan, serialized); }
public void CanChangePlan_should_not_throw_exception_if_same_user_but_other_plan() { var command = new ChangePlan { PlanId = "basic", Actor = new RefToken("user", "me") }; var plan = new AppPlan(command.Actor, "premium"); GuardApp.CanChangePlan(command, plan, appPlans); }
public void CanChangePlan_should_throw_exception_if_plan_is_the_same() { var command = new ChangePlan { PlanId = "free", Actor = new RefToken("user", "me") }; var plan = new AppPlan(new RefToken("user", "me"), "free"); Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void CanChangePlan_should_throw_exception_if_plan_was_configured_from_another_user() { var command = new ChangePlan { PlanId = "free", Actor = new RefToken("user", "me") }; var plan = new AppPlan(new RefToken("user", "other"), "premium"); Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void CanChangePlan_should_throw_exception_if_plan_id_null() { var command = new ChangePlan { Actor = new RefToken("user", "me") }; AppPlan plan = null; Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void CanChangePlan_should_not_throw_exception_if_plan_is_the_same() { var command = new ChangePlan { PlanId = "basic", Actor = new RefToken("user", "me") }; var plan = new AppPlan(command.Actor, "basic"); GuardApp.CanChangePlan(command, App(plan), appPlans); }
public void CanChangePlan_should_throw_exception_if_plan_was_configured_from_another_user() { var command = new ChangePlan { PlanId = "basic", Actor = new RefToken("user", "me") }; var plan = new AppPlan(new RefToken("user", "other"), "premium"); ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("Plan can only changed from the user who configured the plan initially.")); }
public void CanChangePlan_should_throw_exception_if_plan_is_the_same() { var command = new ChangePlan { PlanId = "basic", Actor = new RefToken("user", "me") }; var plan = new AppPlan(command.Actor, "basic"); ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("App has already this plan.")); }
public void CanChangePlan_should_throw_exception_if_plan_not_found() { var command = new ChangePlan { PlanId = "notfound", Actor = new RefToken("user", "me") }; AppPlan plan = null; ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("A plan with this id does not exist.", "PlanId")); }
public void CanChangePlan_should_throw_exception_if_plan_id_is_null() { var command = new ChangePlan { Actor = new RefToken("user", "me") }; AppPlan plan = null; ValidationAssert.Throws(() => GuardApp.CanChangePlan(command, plan, appPlans), new ValidationError("Plan id is required.", "PlanId")); }
public void CanChangePlan_should_throw_exception_if_plan_not_found() { A.CallTo(() => appPlans.GetPlan("free")) .Returns(null); var command = new ChangePlan { PlanId = "free", Actor = new RefToken("user", "me") }; AppPlan plan = null; Assert.Throws <ValidationException>(() => GuardApp.CanChangePlan(command, plan, appPlans)); }
public void ApplyEvent(IEvent @event) { switch (@event) { case AppCreated e: { Roles = Roles.CreateDefaults(e.Name); SimpleMapper.Map(e, this); break; } case AppUpdated e: { SimpleMapper.Map(e, this); break; } case AppImageUploaded e: { Image = e.Image; break; } case AppImageRemoved _: { Image = null; break; } case AppPlanChanged e: { Plan = AppPlan.Build(e.Actor, e.PlanId); break; } case AppPlanReset _: { Plan = null; break; } case AppContributorAssigned e: { Contributors = Contributors.Assign(e.ContributorId, e.Role); break; } case AppContributorRemoved e: { Contributors = Contributors.Remove(e.ContributorId); break; } case AppClientAttached e: { Clients = Clients.Add(e.Id, e.Secret); break; } case AppClientUpdated e: { Clients = Clients.Update(e.Id, e.Role); break; } case AppClientRenamed e: { Clients = Clients.Rename(e.Id, e.Name); break; } case AppClientRevoked e: { Clients = Clients.Revoke(e.Id); break; } case AppWorkflowAdded e: { Workflows = Workflows.Add(e.WorkflowId, e.Name); break; } case AppWorkflowUpdated e: { Workflows = Workflows.Update(e.WorkflowId, e.Workflow); break; } case AppWorkflowDeleted e: { Workflows = Workflows.Remove(e.WorkflowId); break; } case AppPatternAdded e: { Patterns = Patterns.Add(e.PatternId, e.Name, e.Pattern, e.Message); break; } case AppPatternDeleted e: { Patterns = Patterns.Remove(e.PatternId); break; } case AppPatternUpdated e: { Patterns = Patterns.Update(e.PatternId, e.Name, e.Pattern, e.Message); break; } case AppRoleAdded e: { Roles = Roles.Add(e.Name); break; } case AppRoleDeleted e: { Roles = Roles.Remove(e.Name); break; } case AppRoleUpdated e: { Roles = Roles.Update(e.Name, e.Permissions.Prefix(Name)); break; } case AppLanguageAdded e: { LanguagesConfig = LanguagesConfig.Set(e.Language); break; } case AppLanguageRemoved e: { LanguagesConfig = LanguagesConfig.Remove(e.Language); break; } case AppLanguageUpdated e: { LanguagesConfig = LanguagesConfig.Set(e.Language, e.IsOptional, e.Fallback); if (e.IsMaster) { LanguagesConfig = LanguagesConfig.MakeMaster(e.Language); } break; } case AppArchived _: { Plan = null; IsArchived = true; break; } } }
protected void On(AppPlanChanged @event) { plan = string.IsNullOrWhiteSpace(@event.PlanId) ? null : new AppPlan(@event.Actor, @event.PlanId); }
protected void On(AppPlanChanged @event) { Plan = AppPlan.Build(@event.Actor, @event.PlanId); }
public override bool ApplyEvent(IEvent @event) { switch (@event) { case AppCreated e: { SimpleMapper.Map(e, this); return(true); } case AppUpdated e when !string.Equals(e.Label, Label) || !string.Equals(e.Description, Description): { SimpleMapper.Map(e, this); return(true); } case AppImageUploaded e: return(UpdateImage(e, ev => ev.Image)); case AppImageRemoved e when Image != null: return(UpdateImage(e, ev => null)); case AppPlanChanged e when !string.Equals(Plan?.PlanId, e.PlanId): return(UpdatePlan(e, ev => AppPlan.Build(ev.Actor, ev.PlanId))); case AppPlanReset e when Plan != null: return(UpdatePlan(e, ev => null)); case AppContributorAssigned e: return(UpdateContributors(e, (ev, c) => c.Assign(ev.ContributorId, ev.Role))); case AppContributorRemoved e: return(UpdateContributors(e, (ev, c) => c.Remove(ev.ContributorId))); case AppClientAttached e: return(UpdateClients(e, (ev, c) => c.Add(ev.Id, ev.Secret))); case AppClientUpdated e: return(UpdateClients(e, (ev, c) => c.Update(ev.Id, ev.Role))); case AppClientRenamed e: return(UpdateClients(e, (ev, c) => c.Rename(ev.Id, ev.Name))); case AppClientRevoked e: return(UpdateClients(e, (ev, c) => c.Revoke(ev.Id))); case AppWorkflowAdded e: return(UpdateWorkflows(e, (ev, w) => w.Add(ev.WorkflowId, ev.Name))); case AppWorkflowUpdated e: return(UpdateWorkflows(e, (ev, w) => w.Update(ev.WorkflowId, ev.Workflow))); case AppWorkflowDeleted e: return(UpdateWorkflows(e, (ev, w) => w.Remove(ev.WorkflowId))); case AppPatternAdded e: return(UpdatePatterns(e, (ev, p) => p.Add(ev.PatternId, ev.Name, ev.Pattern, ev.Message))); case AppPatternDeleted e: return(UpdatePatterns(e, (ev, p) => p.Remove(ev.PatternId))); case AppPatternUpdated e: return(UpdatePatterns(e, (ev, p) => p.Update(ev.PatternId, ev.Name, ev.Pattern, ev.Message))); case AppRoleAdded e: return(UpdateRoles(e, (ev, r) => r.Add(ev.Name))); case AppRoleUpdated e: return(UpdateRoles(e, (ev, r) => r.Update(ev.Name, ev.Permissions))); case AppRoleDeleted e: return(UpdateRoles(e, (ev, r) => r.Remove(ev.Name))); case AppLanguageAdded e: return(UpdateLanguages(e, (ev, l) => l.Set(ev.Language))); case AppLanguageRemoved e: return(UpdateLanguages(e, (ev, l) => l.Remove(ev.Language))); case AppLanguageUpdated e: return(UpdateLanguages(e, (ev, l) => { l = l.Set(ev.Language, ev.IsOptional, ev.Fallback); if (ev.IsMaster) { LanguagesConfig = LanguagesConfig.MakeMaster(ev.Language); } return l; })); case AppArchived _: { Plan = null; IsArchived = true; return(true); } } return(false); }