public async Task DeleteAppleVarietyAsync(int id)
        {
            AppleVariety appleVariety = new AppleVariety()
            {
                Id = id
            };

            _dbContext.AppleVarieties.Attach(appleVariety);
            _dbContext.AppleVarieties.Remove(appleVariety);

            await _dbContext.SaveChangesAsync();
        }
        public async Task <AppleVariety> CreateAppleVarietyAsync(AppleVariety appleVariety)
        {
            var regions = await _dbContext.Regions.ToListAsync();

            appleVariety.Regions   = regions.Where(r => appleVariety.RegionIds.Any(regionId => regionId == r.Id)).ToList();
            appleVariety.RegionIds = null;

            _dbContext.AppleVarieties.Add(appleVariety);
            await _dbContext.SaveChangesAsync();

            return(appleVariety);
        }
        public async Task <AppleVariety> UpdateAppleVarietyAsync(int id, AppleVariety appleVariety)
        {
            var regions = await _dbContext.Regions.ToListAsync();

            var newAppleVariety = await _dbContext.AppleVarieties.FindAsync(id);

            newAppleVariety.Name      = appleVariety.Name;
            newAppleVariety.AvgWeight = appleVariety.AvgWeight;
            newAppleVariety.Regions   = regions.Where(r => appleVariety.RegionIds.Any(regionId => regionId == r.Id)).ToList();

            await _dbContext.SaveChangesAsync();

            return(appleVariety);
        }
Ejemplo n.º 4
0
        public async Task <IHttpActionResult> Put(int id, AppleVariety appleVariety)
        {
            await _appleVarietyService.UpdateAppleVarietyAsync(id, appleVariety);

            return(Ok());
        }
Ejemplo n.º 5
0
        public async Task <IHttpActionResult> Post([FromBody] AppleVariety appleVariety)
        {
            var createdAppleVariety = await _appleVarietyService.CreateAppleVarietyAsync(appleVariety);

            return(Ok(createdAppleVariety));
        }