예제 #1
0
        public async Task <ActionResult <Map> > PostMap(StationMapDTO stationMap)
        {
            var userIdClaim = int.Parse(User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value ?? "-1");

            if (userIdClaim < 0)
            {
                return(Forbid());
            }

            var dbStationMap = new StationMap
            {
                SharingLinkName = stationMap.SharingLinkName,
                Name            = stationMap.Name,
                NameNL          = stationMap.NameNL,
                UserId          = userIdClaim,
                MapGuid         = Guid.NewGuid()
            };
            var stationMapCountriesToFind = stationMap.StationMapCountries.Select(s => s.StationCountryId);

            var countries = await _context.StationCountries.Where(sc => stationMapCountriesToFind.Contains(sc.Id)).ToListAsync();

            dbStationMap.StationMapCountries = stationMap.StationMapCountries.Select(c =>
                                                                                     new StationMapCountry
            {
                StationCountryId = c.StationCountryId,
                IncludeSpecials  = c.IncludeSpecials
            }).ToList();

            _context.StationMaps.Add(dbStationMap);
            await _context.SaveChangesAsync();

            return(Ok(dbStationMap));
        }
예제 #2
0
        public async Task <IActionResult> PutMap(StationMapDTO stationMap)
        {
            var userIdClaim = int.Parse(User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value ?? "-1");

            if (userIdClaim < 0)
            {
                return(Forbid());
            }
            var dbStationMap = await _context.StationMaps.Where(m => m.UserId == userIdClaim).Include(s => s.StationMapCountries)
                               .SingleOrDefaultAsync(m => m.StationMapId == stationMap.StationMapId);

            if (dbStationMap is null)
            {
                return(NotFound());
            }
            dbStationMap.SharingLinkName = stationMap.SharingLinkName;
            dbStationMap.Name            = stationMap.Name;
            dbStationMap.NameNL          = stationMap.NameNL;

            var stationMapCountriesToFind = stationMap.StationMapCountries.Select(s => s.StationCountryId);
            var countries = await _context.StationCountries.Where(sc => stationMapCountriesToFind.Contains(sc.Id)).ToListAsync();

            dbStationMap.StationMapCountries = stationMap.StationMapCountries.Select(c =>
                                                                                     new StationMapCountry
            {
                StationCountryId = c.StationCountryId,
                IncludeSpecials  = c.IncludeSpecials
            }).ToList();

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MapExists(stationMap.StationMapId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }