Example #1
0
        public async Task <ActionResponse> UpdateAsync(Dictionary <string, object> model)
        {
            var id      = Guid.Parse(model["id"].ToString());
            var captain = await _grpcService.GetByIdAsync(id);

            if (model.TryGetValue("status", out var status))
            {
                var captainNewStatus = (CaptainStatus)int.Parse(status.ToString());
                if (captainNewStatus == CaptainStatus.HasTeam || captainNewStatus == CaptainStatus.Unassigned)
                {
                    return(ActionResponse.InvalidStatus(
                               $"{CaptainStatus.Deleted.ToString()}, {CaptainStatus.Unknown.ToString()}," +
                               $" {CaptainStatus.Retired.ToString()}"));
                }

                if (captainNewStatus == CaptainStatus.Deleted && captain.Status != CaptainStatus.Unassigned)
                {
                    return(ActionResponse.CantDelete(nameof(Captain)));
                }
            }

            if (captain.Id != id)
            {
                return(ActionResponse.NotFound("Captain"));
            }

            UpdateObjectByReflection.SetProperties(model, captain);

            return(await _grpcService.UpdateAsync(captain));
        }
Example #2
0
        public async Task <ActionResponse> UpdateAsync(Dictionary <string, object> model)
        {
            var id      = Guid.Parse(model["id"].ToString());
            var shuttle = await _grpcService.GetByIdAsync(id);

            if (model.TryGetValue("status", out var status))
            {
                var shuttleNewStatus = (ShuttleStatus)int.Parse(status.ToString());
                if (shuttleNewStatus == ShuttleStatus.Assigned || shuttleNewStatus == ShuttleStatus.Unassigned)
                {
                    return(ActionResponse.InvalidStatus($"{ShuttleStatus.Deleted.ToString()}, {ShuttleStatus.Broken.ToString()}," +
                                                        $" {ShuttleStatus.Off.ToString()}, {ShuttleStatus.On.ToString()}"));
                }

                if (shuttleNewStatus == ShuttleStatus.Deleted && shuttle.Status != ShuttleStatus.Unassigned)
                {
                    return(ActionResponse.CantDelete(nameof(Shuttle)));
                }
            }

            if (shuttle.Id != id)
            {
                return(ActionResponse.NotFound("Shuttle"));
            }

            UpdateObjectByReflection.SetProperties(model, shuttle);
            return(await _grpcService.UpdateAsync(shuttle));
        }
Example #3
0
        public async Task <ActionResponse> UpdateAsync(Dictionary <string, object> model)
        {
            var id    = Guid.Parse(model["id"].ToString());
            var robot = await _grpcService.GetByIdAsync(id);

            if (model.TryGetValue("status", out var status))
            {
                var robotNewStatus = (RobotStatus)int.Parse(status.ToString());
                if (robotNewStatus == RobotStatus.Assigned || robotNewStatus == RobotStatus.Unassigned)
                {
                    return(ActionResponse.InvalidStatus($"{RobotStatus.On.ToString()}, {RobotStatus.Off.ToString()}," +
                                                        $" {RobotStatus.Exploring.ToString()}, {RobotStatus.Deleted.ToString()}" +
                                                        $", {RobotStatus.Broken.ToString()}"));
                }
                if (robotNewStatus == RobotStatus.Deleted && robot.Status != RobotStatus.Unassigned)
                {
                    return(ActionResponse.CantDelete(nameof(Robot)));
                }
            }

            if (robot.Id != id)
            {
                return(ActionResponse.NotFound("Robot"));
            }

            UpdateObjectByReflection.SetProperties(model, robot);
            return(await _grpcService.UpdateAsync(robot));
        }
        public async Task <ActionResponse> UpdateAsync(Exploration model)
        {
            switch (model.Status)
            {
            case ExplorationStatus.EnRouteToPlanet:
                return(ActionResponse.NotFound(nameof(Exploration)));

            case ExplorationStatus.Exploring:
            {
                // simulate a complex calculation between robots units covered, planet units, and robot speed
                var explorationTime = new Random().Next(10, 30);
                model.PhaseFinishTime = DateTime.UtcNow.AddSeconds(explorationTime);
                var team = await _teamService.GetByIdAsync(model.TeamId);

                foreach (var robotId in team.Robots)
                {
                    var robot = await _robotService.GetByIdAsync(robotId);

                    robot.Status = RobotStatus.Exploring;
                    await _robotService.UpdateStatusAsync(robot);
                }

                break;
            }

            case ExplorationStatus.Finished:
            {
                var team = await _teamService.GetByIdAsync(model.TeamId);

                foreach (var robotId in team.Robots)
                {
                    var robot = await _robotService.GetByIdAsync(robotId);

                    //simulate result
                    var isOk = new Random().Next(1, 100) % 2 + 1;
                    model.RobotsReports.Add((ExplorationResultStatus)(isOk));
                    model.PhaseFinishTime = null;
                    robot.Status          = RobotStatus.Off;
                    await _robotService.UpdateStatusAsync(robot);
                }

                team.Status = TeamStatus.Ready;
                await _teamService.UpdateStatusAsync(team);

                // here send alert to captain that the mission has finished
                break;
            }
            }


            model.UpdatedAt = DateTime.UtcNow;
            return(await _repository.UpdateAsync(model));
        }
        public async Task <ActionResponse> UpdateAsync(Dictionary <string, object> model)
        {
            var id          = Guid.Parse(model["id"].ToString());
            var exploration = await _grpcService.GetByIdAsync(id);

            if (exploration.Id != id)
            {
                return(ActionResponse.NotFound("Exploration"));
            }

            UpdateObjectByReflection.SetProperties(model, exploration);
            return(await _grpcService.UpdateAsync(exploration));
        }
        public async Task <ActionResponse> AddAsync(Exploration model)
        {
            var planet = _planetService.GetById(model.PlanetId);

            if (planet == null)
            {
                return(ActionResponse.NotFound(nameof(Planet)));
            }

            var team = await _teamService.GetByIdAsync(model.TeamId);

            if (team.Id == Guid.Empty)
            {
                return(ActionResponse.NotFound(nameof(Team)));
            }
            if (team.Status != TeamStatus.Ready)
            {
                return new ActionResponse
                       {
                           Success = false,
                           Errors  = new List <ActionError>
                           {
                               new ActionError
                               {
                                   Code        = "TeamNotReady",
                                   Description = "The team assigned should be in ready state"
                               }
                           }
                       }
            }
            ;

            team.Status = TeamStatus.InTransit;
            await _teamService.UpdateStatusAsync(team);

            // make sure you have the correct data
            model.Status = ExplorationStatus.EnRouteToPlanet;

            // simulate a complex calculation between the shuttle speed, distance between planets
            var transportDuration = new Random().Next(1, 30);

            model.PhaseFinishTime = DateTime.UtcNow.AddSeconds(transportDuration);
            model.CreatedAt       = model.UpdatedAt = DateTime.UtcNow;

            return(await _repository.AddAsync(model));
        }
Example #7
0
        public async Task <ActionResponse> UpdateAsync(Dictionary <string, object> model)
        {
            if (model.TryGetValue("status", out var status))
            {
                var teamNewStatus = int.Parse(status.ToString());
                if (!(teamNewStatus == (int)TeamStatus.Deleted || teamNewStatus == (int)TeamStatus.Lost))
                {
                    return(ActionResponse.InvalidStatus($"{TeamStatus.Deleted.ToString()}, {TeamStatus.Lost.ToString()}"));
                }
            }
            var id   = Guid.Parse(model["id"].ToString());
            var team = await _grpcService.GetByIdAsync(id);

            if (team.Id != id)
            {
                return(ActionResponse.NotFound("Team"));
            }

            UpdateObjectByReflection.SetProperties(model, team);
            return(await _grpcService.UpdateAsync(team));
        }
Example #8
0
        public async Task <ActionResponse> UpdateAsync(Dictionary <string, object> model)
        {
            if (model.TryGetValue("status", out var status))
            {
                var planetNewStatus = int.Parse(status.ToString());
                if (!(planetNewStatus == (int)PlanetStatus.EnRoute ||
                      planetNewStatus == (int)PlanetStatus.NotExplored))
                {
                    return(ActionResponse.InvalidStatus(
                               $"{PlanetStatus.EnRoute.ToString()}, {PlanetStatus.NotExplored.ToString()},"));
                }
            }
            var id     = Guid.Parse(model["id"].ToString());
            var planet = await _grpcService.GetByIdAsync(id);

            if (planet.Id != id)
            {
                return(ActionResponse.NotFound("Planet"));
            }

            UpdateObjectByReflection.SetProperties(model, planet);
            return(await _grpcService.UpdateAsync(planet));
        }