Esempio n. 1
0
        public async Task <IActionResult> CreateStrategy(int ownerId, StrategyForCreationDto strategyForCreationDto)
        {
            try
            {
                var owner = await _repo.User.GetUser(ownerId, false);

                if (owner.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                {
                    return(Unauthorized());
                }

                strategyForCreationDto.OwnerId = ownerId;

                var strategy = _mapper.Map <Strategy>(strategyForCreationDto);

                _repo.Strategy.AddStrategy(strategy);

                await _repo.Strategy.SaveAllAsync();

                var strategyToReturn = _mapper.Map <StrategyToReturnDto>(strategy);
                return(CreatedAtRoute("GetStrategy", new { id = strategy.Id }, strategyToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateStrategy endpoint: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> CloneStrategy(int ownerId, int stratgeyId)
        {
            try
            {
                var owner = await _repo.User.GetUser(ownerId, false);

                if (owner.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                {
                    return(Unauthorized());
                }

                var strategyFromRepo = await _repo.Strategy.GetStrategy(stratgeyId);

                var newStratgey = new StrategyForCreationDto()
                {
                    Title       = strategyFromRepo.Title + " - clone",
                    OwnerId     = ownerId,
                    Description = strategyFromRepo.Description,
                };

                var strategy = _mapper.Map <Strategy>(newStratgey);

                _repo.Strategy.AddStrategy(strategy);

                await _repo.Strategy.SaveAllAsync();

                var axisListFromRepo = await _repo.Axis.GetAxisListDetailed(stratgeyId);

                foreach (var axis in axisListFromRepo)
                {
                    _repo.Axis.AddAxis(new Axis
                    {
                        Title       = axis.Title,
                        Description = axis.Description,
                        StrategyId  = strategy.Id
                    });
                }

                await _repo.Axis.SaveAllAsync();

                var newAxisListFromRepo = await _repo.Axis.GetAxisList(strategy.Id);

                foreach (var newAxis in newAxisListFromRepo)
                {
                    foreach (var axis in axisListFromRepo)
                    {
                        if (axis.Title == newAxis.Title)
                        {
                            foreach (var ap in axis.AxisPoles)
                            {
                                _repo.AxisPole.AddAxisPole(new AxisPole
                                {
                                    AxisId = newAxis.Id,
                                    PoleId = ap.PoleId,
                                    Weight = ap.Weight
                                });
                            }
                        }
                    }
                }
                await _repo.AxisPole.SaveAllAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CloneStrategy endpoint: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }