Exemple #1
0
        public async Task <Result <int> > Create(RouteCreateModel item)
        {
            try
            {
                if (await _userResolver.IsConfirmed())
                {
                    string line = $"LINESTRING({item.Line})";

                    bool isValidLine = _routeRepository.IsValidLine(line);

                    if (isValidLine)
                    {
                        string point = RouteHelper.GetFirstPoint(item.Line);

                        var user = await _userResolver.GetUser();

                        var infoEntity = new RouteInfoEntity()
                        {
                            Difficulty = item.Difficulty,
                            Name       = item.Name,
                            Geometry   = new RouteGeomEntity()
                            {
                                Line   = line,
                                Status = 1
                            },
                            UserId = user.Id,
                            Point  = new RoutePointEntity()
                            {
                                Point  = $"POINT({point})",
                                Status = 1
                            }
                        };

                        _routeRepository.Add(infoEntity);

                        await _routeRepository.Commit();

                        return(new Result <int>(infoEntity.ID));
                    }
                    else
                    {
                        ErrorResult error = GenerateError("Invalid route line", "Line", "Invalid geometry", ErrorStatus.InvalidModel);
                        return(new Result <int>(error));
                    }
                }
                else
                {
                    ErrorResult error = GenerateError("User is not confirmed", "", "", ErrorStatus.Forbidden);
                    return(new Result <int>(error));
                }
            }
            catch (Exception ex)
            {
                ErrorResult error = GenerateError(ex);
                return(new Result <int>(error));
            }
        }
Exemple #2
0
        public void CreateInvalidLine()
        {
            var mocker             = new RouteServiceMocker();
            RouteCreateModel model = new RouteCreateModel();

            mocker.RepoMock.Setup(r => r.Add(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test create"));
            mocker.RepoMock.Setup(r => r.IsValidLine(It.IsAny <string>())).Returns(() => false);
            mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(true));

            IRouteBLService service = mocker.GetService();

            var result = service.Create(new RouteCreateModel());

            Assert.Equal(ErrorStatus.InvalidModel, result.Result.Error.Status);
            Assert.Equal("Invalid route line", result.Result.Error.Message);
        }
Exemple #3
0
        public async Task <IActionResult> Create([FromBody] RouteCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                return(WrapResponse(new Result <RouteViewModel>(WrapModelStateErrors(ModelState))));
            }

            var result = await _routeService.Create(model);

            if (result.IsOk)
            {
                var             idUrl     = Url.Action("GetById", new { id = result.Content });
                Result <string> urlResult = new Result <string>(idUrl);
                return(WrapResponse(urlResult, HttpStatusCode.Created));
            }

            return(WrapResponse(result, HttpStatusCode.Created));
        }
Exemple #4
0
        public async Task <IActionResult> CreateRoute([FromBody] RouteCreateModel model)
        {
            var routes = await _routeService.GetRoutes();

            Func <Guid?, int?> getRouteId = (id) =>
            {
                return(routes.FirstOrDefault(p => p.Guid == id)?.RouteId);
            };

            tblRoute route = new tblRoute()
            {
                Path          = model.Path,
                ParentRouteId = getRouteId(model.ParentRouteGuid)
            };

            await _routeService.CreateRoute(route);

            return(Ok(new RouteModel()
            {
                Guid = route.Guid,
                Path = route.Path,
                ParentRouteGuid = model.ParentRouteGuid
            }));
        }