BusStopDs.cs 4.91 KB
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
    public class BusStopDs : IBusStopDs
    {
        private PostgresDbContext _context;
        public BusStopDs(){
            _context = new PostgresDbContext();
        }
        public Task<IList<BusStopEditDsM>> GetIndexListAsync(){
            return Task.Factory.StartNew(GetAllBusStop);
        }
        private IList<BusStopEditDsM> GetAllBusStop()
        {
           return _context.BusStop.Select(busStop => new BusStopEditDsM
            {
                BusStopId = busStop.BusStopId,
                RoadId = busStop.RoadId,
                RegionId = busStop.RegionId,
                SettlementId = busStop.SettlementId,
                LocationLeft = busStop.LocationLeft,
                LocationRight = busStop.LocationRight,
                SurfaceTypeId = busStop.SurfaceTypeId,
                AreaStopAvailability = busStop.AreaStopAvailability,
                AreaLandAvailability = busStop.AreaLandAvailability,
                PocketAvailability = busStop.PocketAvailability,
                ToiletAvailability = busStop.ToiletAvailability,
                YearBuild = busStop.YearBuild,
                YearRepair = busStop.YearRepair,
                StateCommonId = busStop.StateCommonId
            }).OrderByDescending(BusStop => BusStop.BusStopId).ToList();
        }

        public Task<BusStop> SaveAsync(BusStopEditDsF busStop, int? id = null){
              return Task.Factory.StartNew(()=> { return Save(busStop, id); });
        }
        private BusStop Save(BusStopEditDsF busStop, int? id)
        {   
            BusStop Bs = new BusStop{
                RoadId = busStop.roadId,
                RegionId = busStop.regionId,
                SettlementId = busStop.settlementId,
                LocationLeft = busStop.locationLeft,
                LocationRight = busStop.locationRight,
                SurfaceTypeId = busStop.surfaceTypeId,
                AreaStopAvailability = busStop.areaStopAvailability,
                AreaLandAvailability = busStop.areaLandAvailability,
                PocketAvailability = busStop.pocketAvailability,
                ToiletAvailability = busStop.toiletAvailability,
                YearBuild = busStop.yearBuild,
                YearRepair = busStop.yearRepair,
                StateCommonId = busStop.stateCommonId
            };
            var busStopFromDb = _context.BusStop.FirstOrDefault(x => x.BusStopId == id);
            if(busStopFromDb != null)
            {
                busStopFromDb.RoadId = busStop.roadId;
                busStopFromDb.RegionId = busStop.regionId;
                busStopFromDb.SettlementId = busStop.settlementId;
                busStopFromDb.LocationLeft = busStop.locationLeft;
                busStopFromDb.LocationRight = busStop.locationRight;
                busStopFromDb.SurfaceTypeId = busStop.surfaceTypeId;
                busStopFromDb.AreaStopAvailability = busStop.areaStopAvailability;
                busStopFromDb.AreaLandAvailability = busStop.areaLandAvailability;
                busStopFromDb.PocketAvailability = busStop.pocketAvailability;
                busStopFromDb.ToiletAvailability = busStop.toiletAvailability;
                busStopFromDb.YearBuild = busStop.yearBuild;
                busStopFromDb.YearRepair = busStop.yearRepair;
                busStopFromDb.StateCommonId = busStop.stateCommonId;
            }
            else
            {
                _context.BusStop.Add(Bs);
            }
            _context.SaveChanges();
            return Bs;
        }
        public Task<BusStopDetailsDsM> FindOneDetailsAsync(int Id){
             return Task.Factory.StartNew(()=> { return FindOneDetails(Id); });
        }
        private BusStopDetailsDsM FindOneDetails(int Id){
           return _context.BusStop.Where(x => x.BusStopId == Id).Select(x => new BusStopDetailsDsM{
                BusStopId = x.BusStopId,
                Road = x.Road.Name, 
                Region = x.Region.Name,
                Settlement = x.Settlement.Name,
                LocationLeft = x.LocationLeft,
                LocationRight = x.LocationRight,
                StateCommon = x.StateCommon.Value,
                AreaStopAvailability = x.AreaStopAvailability,
                AreaLandAvailability = x.AreaLandAvailability,
                PocketAvailability = x.PocketAvailability,
                ToiletAvailability = x.ToiletAvailability,
                YearBuild = x.YearBuild,
                YearRepair = x.YearRepair
           }).Single();
        }
        public async Task<int> DeleteAsync(int? Id)
        {
            var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.BusStopId == Id);
            _context.BusStop.Remove(busStop);
            return await _context.SaveChangesAsync();
        }
    }
}