Beispiel #1
0
        /// <summary>
        /// Method to Delete all route step and tag of a route
        /// </summary>
        /// <param name="idRoute">id of the route</param>
        public async Task <ApiResponse> DeleteAllFromRoute(int idRoute)
        {
            try
            {
                await _routeStepBusiness.DeleteFromRoute(await _context.Routesteps.Where(x => x.IdRoute == idRoute).ToListAsync());

                await _routeTagBusiness.DeleteFromRoute(await _context.Routetags.Where(x => x.IdRoute == idRoute).ToListAsync());

                await _context.SaveChangesAsync();

                return(new ApiResponse {
                    Status = ApiStatus.Ok, Message = ApiAction.Delete
                });
            }
            catch (Exception e)
            {
                return(TranslateError.Convert(e));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Method that will Update the route passed in the parameters to the database
        /// </summary>
        /// <param name="step">Object Route to Update</param>
        public async Task <ApiResponse> Update(Route route)
        {
            try
            {
                if (!await _gameRepository.ContainsRoute(route.Id))
                {
                    _context.Routes.Update(_routeBusiness.SeparateStep(route));
                    await _context.SaveChangesAsync();

                    var currentIdRouteStep = await _context.Routesteps.Where(r => r.IdRoute == route.Id).Select(r => r.IdStep).ToListAsync();

                    var idRouteStep             = route.RouteSteps.Select(r => r.IdStep).ToList();
                    var oldIdRouteStep          = currentIdRouteStep.Where(r => !idRouteStep.Contains(r)).ToList();
                    var responseRouteStepDelete = await _routeStepBusiness.DeleteFromRoute(await _context.Routesteps.Where(m => m.IdRoute == route.Id && oldIdRouteStep.Contains(m.IdStep)).Include(rs => rs.TeamRoutes).ToListAsync());

                    if (responseRouteStepDelete.Status == ApiStatus.Ok)
                    {
                        var currentIdRouteTag = await _context.Routetags.Where(r => r.IdRoute == route.Id).Select(r => r.IdTag).ToListAsync();

                        var idRouteTag             = route.RouteTags.Select(r => r.IdTag).ToList();
                        var oldIdRouteTag          = currentIdRouteTag.Where(r => !idRouteTag.Contains(r)).ToList();
                        var responseRouteTagDelete = await _routeTagBusiness.DeleteFromRoute(await _context.Routetags.Where(m => m.IdRoute == route.Id && oldIdRouteTag.Contains(m.IdTag)).ToListAsync());

                        if (responseRouteTagDelete.Status == ApiStatus.Ok)
                        {
                            var responseRouteStepAdd = await _routeStepBusiness.SetUp(route.RouteSteps.Where(m => !currentIdRouteStep.Contains(m.IdStep)).ToList(), route.Id);

                            if (responseRouteStepAdd.Status == ApiStatus.Ok)
                            {
                                var responseRouteTagAdd = await _routeTagBusiness.SetUp(route.RouteTags.Where(m => !currentIdRouteTag.Contains(m.IdTag)).ToList(), route.Id);

                                if (responseRouteTagAdd.Status == ApiStatus.Ok)
                                {
                                    return(new ApiResponse {
                                        Status = ApiStatus.Ok, Message = ApiAction.Update, Response = await this.Find(route.Id)
                                    });
                                }
                                else
                                {
                                    return(responseRouteTagAdd);
                                }
                            }
                            else
                            {
                                return(responseRouteStepAdd);
                            }
                        }
                        else
                        {
                            return(responseRouteTagDelete);
                        }
                    }
                    else
                    {
                        return(responseRouteStepDelete);
                    }
                }
                else
                {
                    return new ApiResponse {
                               Status = ApiStatus.CantDelete, Message = "Impossible de modifier ce parcours, il est déjà utilisé dans un jeu !"
                    }
                };
            }
            catch (Exception e)
            {
                return(TranslateError.Convert(e));
            }
        }