コード例 #1
0
ファイル: MirrorModule.cs プロジェクト: pawelros/BlackMirror
        public MirrorModule(IAuthenticationProvider authenticationProvider, IMirrorStore mirrorStore, ISyncStore syncStore, IReflectionStore reflectionStore)
            : base(authenticationProvider, "/mirror")
        {
            this.Get["/"] = parameters =>
            {
                try
                {
                    var userId   = this.CurrentUser.Id;
                    var response = mirrorStore.GetAll();

                    if (response == null)
                    {
                        return(HttpStatusCode.NotFound);
                    }

                    return(this.Response.AsJson(response));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}"] = parameters =>
            {
                try
                {
                    var    user   = this.CurrentUser;
                    string id     = parameters.id;
                    var    mirror = mirrorStore.GetById(id);

                    return(this.Response.AsJson(mirror));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}/reflections"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    int    take;

                    var mirror = mirrorStore.GetById(id);

                    var reflections = Int32.TryParse(this.Request.Query["take"].ToString(), out take)
                                                      ? reflectionStore.GetByMirror(mirror, take)
                                                      : reflectionStore.GetByMirror(mirror);

                    return(this.Response.AsJson(reflections));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}/sync"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    //Thread.Sleep(1000);
                    int take;

                    var mirror = mirrorStore.GetById(id);

                    var reflections = Int32.TryParse(this.Request.Query["take"].ToString(), out take)
                                              ? syncStore.GetByMirror(mirror, take)
                                              : syncStore.GetByMirror(mirror);

                    return(this.Response.AsJson(reflections));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Post["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <MirrorDto>();
                    if (string.IsNullOrEmpty(dto.OwnerId))
                    {
                        dto.OwnerId = user.Id;
                    }

                    var mirror = mirrorStore.Add(dto);

                    return(this.Response.AsJson(mirror));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Put["/{id}"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    var    dto  = this.Bind <MirrorDto>();

                    if (string.IsNullOrEmpty(dto.SourceRepositoryId))
                    {
                        dto.SourceRepositoryId = dto.SourceRepository.Id;
                    }

                    if (string.IsNullOrEmpty(dto.TargetRepositoryId))
                    {
                        dto.TargetRepositoryId = dto.TargetRepository.Id;
                    }

                    if (string.IsNullOrEmpty(dto.OwnerId))
                    {
                        dto.OwnerId = dto.Owner.Id;
                    }

                    var mirror = mirrorStore.GetById(id);

                    if (mirror == null)
                    {
                        return(HttpStatusCode.NotFound);
                    }

                    mirrorStore.Update(id, dto);

                    return(HttpStatusCode.OK);
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Delete["/{id}"] = parameters =>
            {
                var user = this.CurrentUser;

                string id = parameters.id;

                try
                {
                    mirrorStore.Delete(id);
                    return(HttpStatusCode.OK);
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };
        }
コード例 #2
0
ファイル: SyncModule.cs プロジェクト: pawelros/BlackMirror
        public SyncModule(IAuthenticationProvider authenticationProvider, ISyncStore store, IReflectionStore reflectionStore, ILogStore log)
            : base(authenticationProvider, "/sync")
        {
            this.Post["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <SynchronizationDto>();

                    var sync = store.Add(dto);
                    return(this.Response.AsJson(sync));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    DynamicDictionary           q       = this.Request.Query;
                    Dictionary <string, string> filters = q.ToDictionary().ToDictionary(t => t.Key, t => t.Value.ToString());

                    var result = store.GetAll(filters);

                    return(this.Response.AsJson(result));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    var    dto  = store.GetById(id);

                    return(this.Response.AsJson(dto));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}/reflections"] = parameters =>
            {
                try
                {
                    var    user   = this.CurrentUser;
                    string syncId = parameters.id;
                    var    sync   = store.GetById(syncId);
                    int    take;
                    var    reflections = Int32.TryParse(this.Request.Query["take"].ToString(), out take)
                                              ? reflectionStore.GetBySynchronization(sync, take)
                                              : reflectionStore.GetBySynchronization(sync);

                    return(this.Response.AsJson(reflections));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Put["/{id}/status"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <UpdateStatusDto>();

                    if (dto != null && dto.Status.HasValue)
                    {
                        try
                        {
                            string id = parameters.id;
                            store.UpdateStatus(id, dto.Status.Value);
                            return(HttpStatusCode.OK);
                        }
                        catch (StatusNotChangedException)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase(ReasonPhrases.StatusAlreadyUpdated)
                                   .WithStatusCode(HttpStatusCode.Conflict));
                        }
                    }

                    else
                    {
                        return(this.Negotiate
                               .WithReasonPhrase(ReasonPhrases.UpdateStatusBadRequest)
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Put["{id}/logs"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <SyncLogDto>();

                    if (dto != null && !string.IsNullOrEmpty(dto.Text))
                    {
                        try
                        {
                            string syncId = parameters.id;
                            var    sync   = store.GetById(syncId);
                            log.LogSync(sync, dto.Text);
                            return(HttpStatusCode.OK);
                        }
                        catch (StatusNotChangedException)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase("Log bad request.")
                                   .WithStatusCode(HttpStatusCode.BadRequest));
                        }
                    }
                    else
                    {
                        return(this.Negotiate
                               .WithReasonPhrase("Log bad request.")
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["{id}/logs"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    try
                    {
                        string syncId = parameters.id;
                        var    sync   = store.GetById(syncId);

                        if (sync == null)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase("Sync not found.")
                                   .WithStatusCode(HttpStatusCode.NotFound));
                        }

                        var l = log.Get(sync);

                        if (l == null)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase("Log not found.")
                                   .WithStatusCode(HttpStatusCode.NotFound));
                        }

                        return(this.Response.AsJson(l));
                    }
                    catch (StatusNotChangedException)
                    {
                        return(this.Negotiate
                               .WithReasonPhrase("Log bad request.")
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };
        }
コード例 #3
0
        public ReflectionModule(IAuthenticationProvider authenticationProvider, IReflectionStore store)
            : base(authenticationProvider, "/reflection")
        {
            this.Get["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var all  = store.GetAll();

                    return(this.Response.AsJson(all));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}"] = parameters =>
            {
                try
                {
                    var    user  = this.CurrentUser;
                    string id    = parameters.id;
                    var    model = store.GetById(id);

                    return(this.Response.AsJson(model));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Post["/"] = parameters =>
            {
                try
                {
                    var user  = this.CurrentUser;
                    var dto   = this.Bind <ReflectionDto>();
                    var model = store.Add(dto);

                    return(this.Response.AsJson(model));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Delete["/{id}"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    store.Delete(id);

                    return(HttpStatusCode.OK);
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };
        }