public async Task AddAsync(DonorDto.Request request) { var donor = DbContext.Donors .Include(x => x.Cities) .FirstOrDefault(x => x.Contact == request.Contact); if (donor == null) { await DbContext.Donors.AddAsync(new Donor { Contact = request.Contact, Cities = DbContext.Cities.Where(x => x.Id == request.CityId).ToList(), BirthDate = request.BirthDate, BloodGroupId = request.BloodGroupId, GenderId = request.GenderId, FirstName = request.FirstName, LastName = request.LastName, DonorStatus = DbContext.DonorStatus.FirstOrDefault(x => x.Id == request.StatusId) }); await DbContext.SaveChangesAsync(); return; } var cityExists = donor.Cities.Any(x => x.Id == request.CityId); if (!cityExists) { var cityToAdd = await DbContext.Cities.FirstOrDefaultAsync(x => x.Id == request.CityId); donor.Cities.Add(cityToAdd); await DbContext.SaveChangesAsync(); } }
public async Task Post([FromBody] DonorDto.Request request) { await _donorRepository.AddAsync(request); }