コード例 #1
0
        public async Task <IActionResult> Add(PlaceDto model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var dbPlaceModel = Mapper.Map <Place>(model);
                    await _dbContext.Places.AddAsync(dbPlaceModel);

                    var dbRatingModel = new Rating
                    {
                        UserId            = model.UserId,
                        PlaceId           = dbPlaceModel.Id,
                        IsAddedByThisUser = true
                    };
                    await _dbContext.Ratings.AddAsync(dbRatingModel);

                    await _dbContext.SaveChangesAsync();

                    model.Id = dbPlaceModel.Id;
                    return(Ok());
                }
                return(BadRequest(ModelState));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #2
0
        public async Task <IActionResult> AddRoute(string userId, RouteDto model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var dbModel = new Route
                {
                    Description = model.Description,
                    Name        = model.Name,
                    IsPublic    = model.IsPublic,
                    UserId      = userId,
                    RoutePlaces = new List <RoutePlace>()
                };

                for (int i = 0; i < model.Places.Count; i++)
                {
                    var dbRoutePolace = new RoutePlace
                    {
                        Order     = i,
                        Longitude = model.Places[i].Lng,
                        Latitude  = model.Places[i].Lat,
                        Route     = dbModel
                    };
                    dbModel.RoutePlaces.Add(dbRoutePolace);
                }

                await _dbContext.Routes.AddAsync(dbModel);

                await _dbContext.SaveChangesAsync();

                var dbRatingModel = new Rating
                {
                    UserId            = dbModel.UserId,
                    Route             = dbModel,
                    IsAddedByThisUser = true
                };
                await _dbContext.Ratings.AddAsync(dbRatingModel);

                await _dbContext.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }