public IActionResult Post([FromBody] Models.Northwind.EmployeeTerritory item)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (item == null)
                {
                    return(BadRequest());
                }

                this.OnEmployeeTerritoryCreated(item);
                this.context.EmployeeTerritories.Add(item);
                this.context.SaveChanges();

                var keyEmployeeID  = item.EmployeeID;
                var keyTerritoryID = item.TerritoryID;
                var itemToReturn   = this.context.EmployeeTerritories
                                     .Where(i => i.EmployeeID == keyEmployeeID && i.TerritoryID == keyTerritoryID)
                                     .Include(i => i.Employee)
                                     .Include(i => i.Territory)
                                     .FirstOrDefault();

                return(new JsonResult(itemToReturn, new Newtonsoft.Json.JsonSerializerSettings
                {
                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
                    DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
                })
                {
                    StatusCode = 201
                });
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Post([FromBody] Models.Northwind.EmployeeTerritory item)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (item == null)
                {
                    return(BadRequest());
                }

                this.OnEmployeeTerritoryCreated(item);
                this.context.EmployeeTerritories.Add(item);
                this.context.SaveChanges();

                var keyEmployeeID  = item.EmployeeID;
                var keyTerritoryID = item.TerritoryID;

                var itemToReturn = this.context.EmployeeTerritories.Where(i => i.EmployeeID == keyEmployeeID && i.TerritoryID == keyTerritoryID);

                Request.QueryString = Request.QueryString.Add("$expand", "Employee,Territory");

                return(new ObjectResult(SingleResult.Create(itemToReturn))
                {
                    StatusCode = 201
                });
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }
 partial void OnEmployeeTerritoryUpdated(Models.Northwind.EmployeeTerritory item);
Ejemplo n.º 4
0
        public IActionResult PutEmployeeTerritory([FromODataUri] int keyEmployeeID, [FromODataUri] string keyTerritoryID, [FromBody] Models.Northwind.EmployeeTerritory newItem)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (newItem == null || (newItem.EmployeeID != keyEmployeeID && newItem.TerritoryID != keyTerritoryID))
                {
                    return(BadRequest());
                }

                this.OnEmployeeTerritoryUpdated(newItem);
                this.context.EmployeeTerritories.Update(newItem);
                this.context.SaveChanges();

                var itemToReturn = this.context.EmployeeTerritories.Where(i => i.EmployeeID == keyEmployeeID && i.TerritoryID == keyTerritoryID);
                Request.QueryString = Request.QueryString.Add("$expand", "Employee,Territory");
                return(new ObjectResult(SingleResult.Create(itemToReturn)));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(BadRequest(ModelState));
            }
        }