public void ChangeContact(string userId, ContactData contactData) { Stage.ActorOf <IUser>(_addressFactory.From(userId)) .AndThenTo(user => user.WithContact(new Contact(contactData.EmailAddress, contactData.TelephoneNumber))) .OtherwiseConsume(noUser => Completes.With(Response.Of(ResponseStatus.NotFound, UserLocation(userId)))) .AndThenConsume(userState => Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized(UserData.From(userState)))); }
public void UnsubscribeFromStream(string streamName, string id) { if (Publishers.TryGetValue(streamName, out var publisher)) { publisher.Unsubscribe(new SseSubscriber(streamName, new SseClient(Context?.ClientContext))); } Completes?.With(Response.Of(Response.ResponseStatus.Ok)); }
public void ChangeName(string userId, NameData nameData) { Stage.ActorOf <IUser>(_addressFactory.From(userId)) .AndThenTo(user => user.WithName(new Name(nameData.Given, nameData.Family))) .OtherwiseConsume(noUser => Completes.With(Response.Of(ResponseStatus.NotFound, UserLocation(userId)))) .AndThenConsume(userState => { _repository.Save(userState); Completes.With(Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized(UserData.From(userState)))); }); }
public void QueryUsers() { var users = new List <UserData>(); foreach (var userState in _repository.Users) { users.Add(UserData.From(userState)); } Completes.With(Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized(users))); }
public void ChangeUser(string userId, UserData userData) { if (userId.EndsWith("123")) { Completes.With(Response.Of(ResponseStatus.PermanentRedirect, Headers.Of(ResponseHeader.Of(ResponseHeader.Location, "/")))); } else { Completes.With(Response.Of(ResponseStatus.Ok)); } }
public void QueryUser(string userId) { var userState = _repository.UserOf(userId); if (userState.DoesNotExist()) { Completes.With(Response.Of(ResponseStatus.NotFound, UserLocation(userId))); } else { Completes.With(Response.Of(ResponseStatus.Ok, JsonSerialization.Serialized(UserData.From(userState)))); } }
public void Feed(string feedName, string feedProductId, Type feedProducerClass, int feedProductElements) { var producer = FeedProducer(feedName, feedProducerClass); if (producer == null) { Completes?.With(Response.Of(ResponseStatus.NotFound, $"Feed '{feedName}' does not exist.")); } else { producer.ProduceFeedFor(new FeedProductRequest(Context, feedName, feedProductId, feedProductElements)); } }
/// <summary> /// Completes with <code>Ok</code> and the file content or <code>NotFound</code>. /// </summary> /// <param name="contentFile">The name of the content file to be served</param> /// <param name="root">The root path of the static content</param> /// <param name="validSubPaths">The indicating the valid file paths under the root</param> public void ServeFile(string contentFile, string root, string validSubPaths) { if (string.IsNullOrWhiteSpace(_rootPath)) { var slash = root.EndsWith("/") ? "" : "/"; _rootPath = root + slash; } var uri = string.IsNullOrEmpty(contentFile) ? "/index.html" : Context?.Request?.Uri?.AbsolutePath; var contentPath = ContentFilePath(_rootPath + uri); try { // try to read from disk first if (FileExists(contentFile)) { var fileContent = ReadFile(contentPath); Completes?.With(Response.Of(ResponseStatus.Ok, Body.From(fileContent, Body.Encoding.UTF8).Content)); } else // than from embedded resource { _rootPath = root.EndsWith("/") ? root.Substring(0, root.Length - 1) : root; uri = string.IsNullOrEmpty(contentFile) ? "/index.html" : Context?.Request?.Uri?.AbsolutePath; _assembly ??= LoadFromPath(root); var response = new List <string> { $"{_rootPath}{uri}", $"{WithIndexHtmlAppended(_rootPath + uri)}" } .Select(CleanPath) .Where(IsValidFilename) .Take(1) .Select(FileResponse) .DefaultIfEmpty(NotFound()); Completes?.With(response.First()); } } catch (IOException) { Completes?.With(Response.Of(ResponseStatus.InternalServerError)); } catch (ArgumentException) { Completes?.With(Response.Of(ResponseStatus.NotFound)); } }
public void Register(UserData userData) { var userAddress = _addressFactory.UniquePrefixedWith("u-"); var userState = UserStateFactory.From( userAddress.IdString, Name.From(userData.NameData.Given, userData.NameData.Family), Contact.From(userData.ContactData.EmailAddress, userData.ContactData.TelephoneNumber)); Stage.ActorFor <IUser>(() => new UserActor(userState), userAddress); _repository.Save(userState); Completes.With(Response.Of( ResponseStatus.Created, Headers.Of(ResponseHeader.Of(ResponseHeader.Location, UserLocation(userState.Id))), JsonSerialization.Serialized(UserData.From(userState)))); }
public void SubscribeToStream(string streamName, Type feedClass, int feedPayload, int feedInterval, string feedDefaultId) { var clientContext = Context?.ClientContext; clientContext?.WhenClosing(UnsubscribeRequest() !); var correlationId = Context?.Request?.HeaderValueOr(RequestHeader.XCorrelationID, string.Empty); var subscriber = new SseSubscriber( streamName, new SseClient(clientContext), correlationId, Context?.Request?.HeaderValueOr(RequestHeader.LastEventID, string.Empty)); PublisherFor(streamName, feedClass, feedPayload, feedInterval, feedDefaultId).Subscribe(subscriber); Completes?.With(Response.Of(Response.ResponseStatus.Ok, ResponseHeader.WithHeaders(ResponseHeader.WithCorrelationId(correlationId)))); }
public void ServeFile(string contentFile, string root, string validSubPaths) { if (_rootPath == null) { var slash = root.EndsWith("/") ? "" : "/"; _rootPath = root + slash; } var contentPath = _rootPath + Context?.Request?.Uri?.AbsolutePath; try { var fileContent = ReadFile(contentPath); Completes?.With(Response.Of(Response.ResponseStatus.Ok, Body.From(fileContent, Body.Encoding.UTF8).Content)); } catch (IOException) { Completes?.With(Response.Of(Response.ResponseStatus.InternalServerError)); } catch (ArgumentException) { Completes?.With(Response.Of(Response.ResponseStatus.NotFound)); } }