public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            if (customer.Name != null)
            {
                _context.Entry(customer.Name).State = EntityState.Modified;
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 2
0
        public async Task <IActionResult> PutAgent(long id, Agent agent)
        {
            if (id != agent.Id)
            {
                return(BadRequest());
            }

            _context.Entry(agent).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AgentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutAgent(int id, Agent agent)
        {
            if (id != agent.Id)
            {
                return(BadRequest());
            }

            _context.Entry(agent).State = EntityState.Modified;

            if (agent.Phone != null)
            {
                _context.Entry(agent.Phone).State = EntityState.Modified;
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AgentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            await _context.Phones.ToListAsync();

            var existingAgents = await _context.Agents.FindAsync(id);

            return(NoContent());
        }
        public ActionResult UpdateAccommodation(Accommodation acc)
        {
            var oldAcc = _context.Accommodations
                         .Include(accommodation => accommodation.AdditionalServices)
                         .Include(accommodation => accommodation.Location)
                         .Include(accommodation => accommodation.AccommodationType)
                         .Include(accommodation => accommodation.Unavailabilities)
                         .Include(accommodation => accommodation.PeriodPrices)
                         .FirstOrDefault(accommodation => accommodation.Id == acc.Id);

            if (oldAcc == null)
            {
                return(NotFound());
            }

            AccommodationPortClient    accPortClient = new AccommodationPortClient();
            updateAccommodationRequest accRequest    = new updateAccommodationRequest();

            AccommodationDTO accDTO = new AccommodationDTO(acc);
            List <AccommodationService.AdditionalService> ads = new List <AccommodationService.AdditionalService>();

            for (int i = 0; i < acc.AdditionalServices.Count; ++i)
            {
                AgentDB.Models.AdditionalServicesOnly  additionalService = _context.AdditionalServicesOnlies.FirstOrDefault(addServ => addServ.AdditionalServiceName == acc.AdditionalServices[i].AdditionalServiceName);
                AccommodationService.AdditionalService addService        = new AccommodationService.AdditionalService();
                addService.id = additionalService.Id;
                addService.additionalServiceName = additionalService.AdditionalServiceName;
                ads.Add(addService);
            }

            accDTO.additionalServices = ads.ToArray();

            accRequest.AccommodationDTO = accDTO;

            var accUpdate = accPortClient.updateAccommodationAsync(accRequest);

            accUpdate.Wait();

            // update accommodation details
            _context.Entry(oldAcc).CurrentValues.SetValues(acc);

            // delete old services
            foreach (var oldServ in oldAcc.AdditionalServices.ToList())
            {
                bool found = false;
                if (acc.AdditionalServices.Any(s => s.AdditionalServiceName == oldServ.AdditionalServiceName))
                {
                    found = true;
                }

                if (!found)
                {
                    oldAcc.AdditionalServices.Remove(oldServ);
                    _context.AdditionalServices.Remove(oldServ);
                }
            }

            // add new services
            foreach (var newServ in acc.AdditionalServices)
            {
                bool found = false;
                if (oldAcc.AdditionalServices.Any(s => s.AdditionalServiceName == newServ.AdditionalServiceName))
                {
                    found = true;
                }

                if (!found)
                {
                    Models.AdditionalService service = new Models.AdditionalService();
                    service.AdditionalServiceName = newServ.AdditionalServiceName;
                    oldAcc.AdditionalServices.Add(service);
                }
            }

            // add new period price
            foreach (var newPeriod in acc.PeriodPrices)
            {
                bool found = false;
                if (oldAcc.PeriodPrices.Any(p => p.Id == newPeriod.Id))
                {
                    found = true;
                }

                if (!found)
                {
                    oldAcc.PeriodPrices.Add(newPeriod);
                }
            }

            // add new unavailability
            foreach (var newUnv in acc.Unavailabilities)
            {
                bool found = false;
                if (oldAcc.Unavailabilities.Any(u => u.Id == newUnv.Id))
                {
                    found = true;
                }

                if (!found)
                {
                    oldAcc.Unavailabilities.Add(newUnv);
                }
            }

            _context.Entry(oldAcc).State = EntityState.Modified;
            _context.SaveChanges();
            return(Ok(oldAcc));
        }
Esempio n. 5
0
        public async Task <ActionResult <AgentDto> > PutAgentItem(long id)
        {
            if (!AgentItemExists(id))
            {
                return(BadRequest());
            }

            //read raw and deserialize it to AgentDto object
            if (Request.Body.CanSeek)
            {
                // Reset the position to zero to read from the beginning.
                Request.Body.Position = 0;
            }
            var rawRequestBody = new StreamReader(Request.Body).ReadToEnd();

            AgentDto agent = JsonConvert.DeserializeObject <AgentDto>(rawRequestBody);


            _context.AgentItems.Load();

            if (id != agent.id)
            {
                return(BadRequest());
            }

            AgentItem agentItem = null;

            List <Resource> resources = null;

            //find agentItem in database with given id, then load its resources
            foreach (var a in _context.AgentItems.Local)
            {
                if (a.id == id)
                {
                    agentItem = _context.AgentItems.Single(a => a.id == id);
                    _context.Entry(agentItem).Collection(a => a.resources).Load();
                    resources = a.resources;
                    foreach (var s in resources)
                    {
                        _context.Resources.Remove(s);
                    }
                    break;
                }
            }

            await _context.SaveChangesAsync();

            foreach (var s in agent.resources)
            {
                var resource = new Resource()
                {
                    Name = s
                };
                _context.Resources.Add(resource);
                resources.Add(resource);
            }

            await _context.SaveChangesAsync();

            _context.Entry(agentItem).State = EntityState.Detached;
            agentItem = new AgentItem()
            {
                id        = agent.id,
                name      = agent.name,
                os        = agent.os,
                status    = agent.status,
                type      = agent.type,
                ip        = agent.ip,
                location  = agent.location,
                resources = resources,
            };

            _context.Entry(agentItem).State = EntityState.Modified;
            _context.AgentItems.Update(agentItem);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AgentItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(new AgentDto()
            {
                id = agentItem.id,
                name = agentItem.name,
                os = agentItem.os,
                status = agentItem.status,
                type = agentItem.type,
                ip = agentItem.ip,
                location = agentItem.location,
                resources = agentItem.resources.Select(r => r.Name).ToArray()
            });
        }