public async Task <SeedKotaResponse> Handle(SeedKotaRequest request, CancellationToken cancellationToken) { var result = new SeedKotaResponse(); var dataOld = await _entityRepository.GetAllKota(); // this is temporary just to make sure there is no duplicate data List <Kota> Kotas = new List <Kota>(); // collect provinsi's data from db to temporary List var provinsis = await _context.Provinsis .ToListAsync(cancellationToken); foreach (var data in dataOld) { Kota Kota = new Kota(); Kota = Kotas .SingleOrDefault(x => x.id_kota == data.id_kota); if (Kota == null) { string provinsiID = provinsis.Where(x => x.id_provinsi == data.id_provinsi_fk).FirstOrDefault().ProvinsiID ?? ""; Kota = new Kota { id_kota = data.id_kota, ProvinsiID = provinsiID, Name = data.nama_kota }; Kotas.Add(Kota); _context.Kotas.Add(Kota); } } await _context.SaveChangesAsync(cancellationToken); result.IsSuccessful = true; return(result); }
public async Task <long> AddPost(Kota request) { long result = 0; if (db != null) { try { await db.Kota.AddAsync(request); result = await db.SaveChangesAsync(); } catch (Exception ex) { throw ex; } } return(result); }
public async Task <Kota> GetPost(KotaRequest request) { Kota response = new Kota(); if (db != null) { try { return(await(from model in db.Kota where model.RowStatus == true && model.Id == request.ID select model).FirstOrDefaultAsync()); } catch (Exception ex) { throw ex; } } return(response); }
public async Task <bool> UpdatePost(Kota request) { bool result = false; if (db != null) { try { db.Kota.Update(request); await db.SaveChangesAsync(); result = true; } catch (Exception ex) { throw ex; } } return(result); }