Example #1
0
        public void Update(CommandFactory commandFactory, Core.Visualization.IVisualizer visualizer)
        {
            var describeWorld = commandFactory.ExecuteCommand <DescribeWorldResponse>(string.Empty);

            if (_worldName != describeWorld.Name)
            {
                _worldResponse = commandFactory.ExecuteCommand <WorldResponse>(describeWorld.Name);
                _homeResponse  = commandFactory.ExecuteCommand <HomeResponse>(string.Empty);
                _graph         = new MapGraph(_worldResponse);
                _cars          = new List <Car>();

                _worldName = describeWorld.Name;
            }

            UpdateCarState(commandFactory);
            foreach (var car in _cars.Where(c => c.CanMove))
            {
                if (!car.Route.Any())
                {
                    car.Route = _graph.FindBestRoute(car) ?? new List <Road>();
                }

                car.MoveToNextCity(commandFactory);
            }

            Thread.Sleep(100);
        }
        public IActionResult Home()
        {
            HomeResponse res = new HomeResponse();

            res.message = "Everything is running correctly.";

            return(Ok(res));
        }
        public async Task <IActionResult> Index()
        {
            Tuple <int, List <Tag> > tags = await _tagsService.FetchPageWithImages(1, 3);

            Tuple <int, List <Category> > categories = await _categoriesService.FetchPageWithImages(1, 3);

            return(StatusCodeAndDtoWrapper.BuildSuccess(HomeResponse.Build(tags.Item2, categories.Item2)));
        }
Example #4
0
        public HttpResponseMessage Post([FromBody] HomeRequest request)
        {
            HttpResponseMessage response;

            try
            {
                HomeResponse homeResponse = new HomeResponse();
                homeResponse.AllEvents = new ObservableCollection <TechReady.Common.Models.Event>();

                using (DbModel.TechReadyDbContext ctx = new DbModel.TechReadyDbContext())
                {
                    var events = (from c in ctx.Events
                                  where (c.EventStatus == DbModel.EventStatus.Tentative || c.EventStatus == DbModel.EventStatus.Confirmed) &&
                                  c.EventVisibility == DbModel.EventVisibility.Visible
                                  select c).ToList();


                    foreach (var ev in events)
                    {
                        try
                        {
                            if (ev.EventToDate.HasValue &&
                                (DateTime.Now.Date - ev.EventToDate.Value.Date).TotalDays > 1)
                            {
                                continue;
                            }

                            homeResponse.AllEvents.Add(CreateDxEvent(ev));
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Trace.WriteLine(ex.Message);
                        }
                    }
                }

                response = this.Request.CreateResponse(HttpStatusCode.OK, homeResponse);
                response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
            }
            catch (Exception ex)
            {
                HttpError myCustomError = new HttpError(ex.Message)
                {
                    { "IsSuccess", false }
                };
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, myCustomError));
            }
            return(response);
        }
Example #5
0
        public static RouteBuilder CreateRoutes(
            this RouteBuilder routeBuilder,
            AppConfiguration appConfiguration)
        {
            routeBuilder.MapGetHandler("/", context =>
            {
                var response = new HomeResponse();
                context.GetAuthenticatedLogin()
                .Match(
                    Some: login =>
                {
                    response.Email = login.Email;
                    response.AddLink(Rels.Account, "/account");
                    response.AddLink(Rels.Password, "/password");
                },
                    None: () => response.AddLink(Rels.Login, "/login")
                    );
                return(Task.FromResult(Result <HomeResponse, TardisFault> .Succeed(response)));
            },
                                       authenticated: false);

            routeBuilder.MapDeleteHandler <HomeResponse>("/", context =>
                                                         context.GetAuthenticatedLogin()
                                                         .MatchAsync(
                                                             Some: async login =>
            {
                await Db.DeleteLogin(appConfiguration.ConnectionString, login);
                return((Result <HomeResponse, TardisFault>) new HomeResponse());
            },
                                                             None: () => new HomeResponse()
                                                             )
                                                         );

            routeBuilder.MapPostHandler <RegisterRequest, RegisterResponse>(
                "/",
                async(context, registerRequest) =>
                await registerRequest.Validate()
                .BindAsync(dto => Db.LoginByEmail(appConfiguration.ConnectionString, dto.Email)
                           .Match(
                               Some: _ => Result <RegisterRequest, TardisFault> .Fail(
                                   new TardisFault("Email already exists")),
                               None: () => Result <RegisterRequest, TardisFault> .Succeed(dto)
                               )
                           )
                .Map(dto => dto.ToModel())
                .MapAsync(login => Db.InsertLogin(appConfiguration.ConnectionString, login))
                .Run((Login login) => Authentication.CreateToken(appConfiguration.EncryptionKey, () => DateTimeOffset.Now, login, TokenType.Verification)
                     .Pipe(token => login.CreateVerificationEmail(token, context.Request)
                           .Tee(emailMessage => Email.Send(appConfiguration.GetEmailConfiguration(), emailMessage)))
                     )
                .Map(login => login.ToDto()),
                authenticated: false);

            routeBuilder.MapGetHandler <HomeResponse>(
                "/verify/{token}",
                async context =>
                await context.GetStringRouteValue("token")
                .Bind(token => Authentication.DecryptToken(
                          appConfiguration.EncryptionKey,
                          () => DateTimeOffset.Now,
                          token,
                          TokenType.Verification)
                      .AssertLogin())
                .RunAsync(login => Db.UpdateLoginSetVerified(appConfiguration.ConnectionString, login.LoginId))
                .Map(_ =>
            {
                var response = new HomeResponse();
                response.AddLink(Rels.Login, "/login");
                return(response);
            }),
                authenticated: false);

            routeBuilder.MapPostHandler <LoginRequest, LoginResponse>(
                "/login",
                async(context, loginRequest) =>
                await loginRequest.Validate()
                .BindAsync(dto => Db.LoginByEmail(appConfiguration.ConnectionString, dto.Email)
                           .ToTardisResult(HttpStatusCode.InternalServerError, "Unknown Email or Password."))
                .Bind(login => Password.HashMatches(loginRequest.Password, login.PasswordHash)
                            ? Succeed(login)
                            : Fail(new TardisFault("Unknown Email or Password.")))
                .Map(login => Authentication.CreateToken(appConfiguration.EncryptionKey, () => DateTimeOffset.Now, login))
                .Map(token => new LoginResponse {
                Token = token
            }),
                authenticated: false);

            routeBuilder.MapPutHandler <ChangePasswordRequest, ChangePasswordResponse>(
                "/password",
                async(context, changePasswordRequest) =>
                await changePasswordRequest.Validate()
                .Map(dto => dto.ToModel())
                .BindAsync((PasswordChange model) => context.GetAuthenticatedLogin()
                           .AssertLogin()
                           .MapAsync((Login login) => Db.LoginById(appConfiguration.ConnectionString, login.LoginId))
                           .BindAsync(login => model.AssertOldPasswordMatches(login)
                                      .RunAsync(m => Db.UpdateLoginPassword(appConfiguration.ConnectionString, login.LoginId, m.NewPasswordHash))
                                      )
                           )
                .Map((PasswordChange _) => new ChangePasswordResponse())
                );

            routeBuilder.MapPostHandler <AccountRequest, AccountResponse>(
                "/account",
                async(context, accountRequest) =>
                await accountRequest.Validate()
                .Bind(dto => context.GetAuthenticatedLogin()
                      .AssertLogin()
                      .Map(login => dto.ToModel(login))
                      )
                .MapAsync(account => Db.InsertAccount(appConfiguration.ConnectionString, account))
                .Map(account => account.ToDto())
                );

            routeBuilder.MapGetHandler <AccountResponseCollection>(
                "/account",
                async context =>
                await context.GetAuthenticatedLogin()
                .AssertLogin()
                .MapAsync(login => Db.AccountByLogin(appConfiguration.ConnectionString, login))
                .Map((IEnumerable <Account> accounts) => accounts.ToDto())
                );

            routeBuilder.MapDeleteHandler <AccountResponse>(
                "/account/{accountId}",
                async(context) =>
                await context.GetIntegerRouteValue("accountId")
                .BindAsync(accountId => Db.AccountById(appConfiguration.ConnectionString, accountId)
                           .ToTardisResult(HttpStatusCode.NotFound, "Not Found"))
                .Bind(account => context.GetAuthenticatedLogin().AssertAccount(account))
                .RunAsync((Account account) => Db.DeleteAccount(
                              appConfiguration.ConnectionString,
                              new Account {
                AccountId = account.AccountId
            }))
                .Map(_ => new AccountResponse())
                );

            routeBuilder.MapPostHandler <TransactionRequest, TransactionResponse>(
                "/account/{accountId}/transaction",
                async(context, transactionRequest) =>
                await context.GetIntegerRouteValue("accountId")
                .BindAsync(accountId => Db.AccountById(appConfiguration.ConnectionString, accountId)
                           .ToTardisResult(HttpStatusCode.NotFound, "Not Found")
                           .Bind(account => context.GetAuthenticatedLogin().AssertAccount(account)))
                .MapAsync(account => Db.TransactionsByAccount(appConfiguration.ConnectionString, account)
                          .Map(transactions => transactions.FirstOrDefault() ?? new Transaction())
                          .Map(transaction => transactionRequest.ToModel(account, transaction, DateTimeOffset.Now)))
                .MapAsync(transaction => Db.InsertTransaction(appConfiguration.ConnectionString, transaction))
                .Map(transaction => transaction.ToDto())
                );

            routeBuilder.MapGetHandler <TransactionResponseCollection>(
                "/account/{accountId}/transaction",
                async context =>
                await context.GetIntegerRouteValue("accountId")
                .BindAsync(accountId => Db.AccountById(appConfiguration.ConnectionString, accountId)
                           .ToTardisResult(HttpStatusCode.NotFound, "Not Found"))
                .Bind(account => context.GetAuthenticatedLogin().AssertAccount(account))
                .MapAsync(account => Db.TransactionsByAccount(appConfiguration.ConnectionString, account)
                          .Map(transactions => transactions.ToDto()))
                );

            routeBuilder.MapPostHandler <ScheduleRequest, ScheduleResponse>(
                "/account/{accountId}/schedule",
                async(context, scheduleRequest) =>
                await context.GetIntegerRouteValue("accountId")
                .BindAsync(accountId => Db.AccountById(appConfiguration.ConnectionString, accountId)
                           .ToTardisResult(HttpStatusCode.NotFound, "Not Found")
                           .Bind(account => context.GetAuthenticatedLogin().AssertAccount(account))
                           .Bind(account => scheduleRequest.Validate().Map(sr => sr.ToModel(account))))
                .MapAsync(schedule => Db.InsertSchedule(appConfiguration.ConnectionString, schedule))
                .Map(schedule => schedule.ToDto())
                );

            routeBuilder.MapGetHandler <ScheduleResponseCollection>(
                "/account/{accountId}/schedule",
                async context =>
                await context.GetIntegerRouteValue("accountId")
                .BindAsync(accountId => Db.AccountById(appConfiguration.ConnectionString, accountId)
                           .ToTardisResult(HttpStatusCode.NotFound, "Not Found"))
                .Bind(account => context.GetAuthenticatedLogin().AssertAccount(account))
                .MapAsync(account => Db.ScheduleByAccount(appConfiguration.ConnectionString, account)
                          .Map(schedules => schedules.ToDto()))
                );

            routeBuilder.MapDeleteHandler <ScheduleResponse>(
                "/account/{accountId}/schedule/{scheduleId}",
                async(context) =>
                await context.GetIntegerRouteValue("accountId")
                .BindAsync(accountId => Db.AccountById(appConfiguration.ConnectionString, accountId)
                           .ToTardisResult(HttpStatusCode.NotFound, "Not Found"))
                .Bind(account => context.GetAuthenticatedLogin().AssertAccount(account))
                .BindAsync <Account, TardisFault, int>(account => context.GetIntegerRouteValue("scheduleId")
                                                       .RunAsync((int scheduleId) => Db.DeleteSchedule(
                                                                     appConfiguration.ConnectionString,
                                                                     new Schedule {
                ScheduleId = scheduleId, AccountId = account.AccountId
            })))
                .Map(_ => new ScheduleResponse())
                );

            return(routeBuilder);
        }
 public override void Because()
 {
     response = action.Get(new HomeRequest());
 }
 public override void Because()
 {
     response = action.Get(null);
 }
 public override void Because()
 {
     response = action.Get(new HomeRequest());
 }
 public override void Because()
 {
     response = action.Get(null);
 }