Beispiel #1
0
        public async Task <IHttpActionResult> CreateRef([FromODataUri] int key, string navigationProperty, [FromBody] Uri link)
        {
            DemoTask task = await taskContext.Tasks.SingleOrDefaultAsync(p => p.Id == key);

            if (task == null)
            {
                return(NotFound());
            }
            switch (navigationProperty)
            {
            case "ContactTasks":
                // Note: The code for GetKeyFromUri is shown later in this topic.
                int         relatedKey  = Helpers.GetKeyFromUri <int>(Request, link);
                ContactTask contactTask = await taskContext.ContactTasks.SingleOrDefaultAsync(f => f.Id == relatedKey);

                if (contactTask == null)
                {
                    return(NotFound());
                }
                task.ContactTasks.Add(contactTask);
                break;

            default:
                return(StatusCode(HttpStatusCode.NotImplemented));
            }
            await taskContext.SaveChangesAsync();

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public async Task <IHttpActionResult> DeleteRef([FromODataUri] int key, [FromODataUri] int relatedKey, string navigationProperty)
        {
            DemoTask task = await taskContext.Tasks.SingleOrDefaultAsync(p => p.Id == key);

            if (task == null)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }
            switch (navigationProperty)
            {
            case "ContactTasks":
                int         contactTaskId = Convert.ToInt32(relatedKey);
                ContactTask contactTask   = await taskContext.ContactTasks.SingleOrDefaultAsync(p => p.Id == contactTaskId);

                if (contactTask == null)
                {
                    return(NotFound());
                }
                contactTask.Task = null;
                break;

            default:
                return(StatusCode(HttpStatusCode.NotImplemented));
            }
            return(StatusCode(HttpStatusCode.NoContent));
        }
 public async Task <IHttpActionResult> Put([FromODataUri] int key, ContactTask contactTask)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (key != contactTask.Id)
     {
         return(BadRequest());
     }
     contactTaskContext.Entry(contactTask).State = EntityState.Modified;
     try {
         await contactTaskContext.SaveChangesAsync();
     }
     catch (DbUpdateConcurrencyException) {
         if (!ContactTaskExists(key))
         {
             return(NotFound());
         }
         else
         {
             throw;
         }
     }
     return(Updated(contactTask));
 }
        public async Task <IHttpActionResult> Post(ContactTask contactTask)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            contactTaskContext.ContactTasks.Add(contactTask);
            await contactTaskContext.SaveChangesAsync();

            return(Created(contactTask));
        }
        public IQueryable <DemoTask> GetTask([FromODataUri] int key)
        {
            IQueryable <DemoTask>    result       = Enumerable.Empty <DemoTask>().AsQueryable();
            IQueryable <ContactTask> contactTasks = contactTaskContext.ContactTasks
                                                    .Include(ct => ct.Task).Include(ct => ct.Contact).ThenInclude(c => c.Department).Where(ct => ct.Id == key);

            if (contactTasks.Count() > 0)
            {
                ContactTask contactTask = contactTasks.First();
                if (contactTask.Task != null)
                {
                    result = contactTaskContext.Tasks
                             .Include(c => c.ContactTasks)
                             .Where(d => d.Id == contactTask.Task.Id);
                }
            }
            return(result);
        }
        public async Task <IHttpActionResult> CreateRef([FromODataUri] int key, string navigationProperty, [FromBody] Uri link)
        {
            Contact contact = await contactContext.Contacts.SingleOrDefaultAsync(p => p.Id == key);

            if (contact == null)
            {
                return(NotFound());
            }
            switch (navigationProperty)
            {
            case "Department":
                int        relatedKey = Helpers.GetKeyFromUri <int>(Request, link);
                Department department = await contactContext.Departments.SingleOrDefaultAsync(p => p.Id == relatedKey);

                if (department == null)
                {
                    return(NotFound());
                }
                contact.Department = department;
                break;

            case "ContactTasks":
                relatedKey = Helpers.GetKeyFromUri <int>(Request, link);
                ContactTask task = await contactContext.ContactTasks.SingleOrDefaultAsync(p => p.Id == relatedKey);

                if (task == null)
                {
                    return(NotFound());
                }
                contact.ContactTasks.Add(task);
                break;

            default:
                return(StatusCode(HttpStatusCode.NotImplemented));
            }
            await contactContext.SaveChangesAsync();

            return(StatusCode(HttpStatusCode.NoContent));
        }
        private static void CreateSalesDepartmentModel(EFCoreDemoDbContext dbContext)
        {
            Department salesDepartment = new Department()
            {
                Title  = "Sales",
                Office = "LA"
            };
            DemoTask sellTask = new DemoTask()
            {
                Description      = "Sell",
                Note             = "Good sales are good premium",
                StartDate        = new DateTime(2010, 09, 23),
                DateCompleted    = new DateTime(2016, 12, 31),
                PercentCompleted = 94
            };
            DemoTask manageTask = new DemoTask()
            {
                Description      = "Manage",
                Note             = "Manage personal",
                StartDate        = new DateTime(2007, 02, 03),
                DateCompleted    = new DateTime(2011, 05, 01),
                PercentCompleted = 100
            };
            DemoTask topManagerTask = new DemoTask()
            {
                Description      = "TopManagement",
                Note             = "Manage company",
                StartDate        = new DateTime(2015, 08, 14),
                DateCompleted    = new DateTime(2018, 11, 21),
                PercentCompleted = 39
            };
            Contact seller = new Contact()
            {
                Name       = "Zack",
                Address    = "LA",
                Department = salesDepartment
            };
            Contact manager = new Contact()
            {
                Name       = "Marina",
                Address    = "Moscow",
                Department = salesDepartment
            };
            Contact topManager = new Contact()
            {
                Name       = "Kate",
                Address    = "Madrid",
                Department = salesDepartment
            };
            ContactTask sellContactTask = new ContactTask()
            {
                Contact = seller,
                Task    = sellTask
            };
            ContactTask manageContactTask = new ContactTask()
            {
                Contact = manager,
                Task    = manageTask
            };
            ContactTask topManagerContactTask = new ContactTask()
            {
                Contact = topManager,
                Task    = topManagerTask
            };

            dbContext.Tasks.Add(sellTask);
            dbContext.Tasks.Add(manageTask);
            dbContext.Tasks.Add(topManagerTask);
            dbContext.ContactTasks.Add(sellContactTask);
            dbContext.ContactTasks.Add(manageContactTask);
            dbContext.ContactTasks.Add(topManagerContactTask);
            dbContext.Contacts.Add(seller);
            dbContext.Contacts.Add(manager);
            dbContext.Contacts.Add(topManager);
            dbContext.Departments.Add(salesDepartment);
        }
        private static void CreateITDepartmentModel(EFCoreDemoDbContext dbContext)
        {
            Department itDepartment = new Department()
            {
                Title  = "IT",
                Office = "SiliconValley"
            };
            DemoTask itTask = new DemoTask()
            {
                Description      = "HardCode",
                Note             = "This must be perfect code",
                StartDate        = new DateTime(2016, 01, 23),
                DateCompleted    = new DateTime(2016, 06, 13),
                PercentCompleted = 52
            };
            DemoTask writeTask = new DemoTask()
            {
                Description      = "Write",
                Note             = "Write docs",
                StartDate        = new DateTime(2015, 09, 14),
                DateCompleted    = new DateTime(2018, 07, 18),
                PercentCompleted = 25
            };
            DemoTask designTask = new DemoTask()
            {
                Description      = "Draw",
                Note             = "Draw pictures like Picasso",
                StartDate        = new DateTime(2016, 04, 03),
                DateCompleted    = new DateTime(2020, 11, 04),
                PercentCompleted = 3
            };
            Contact developer = new Contact()
            {
                Name    = "John",
                Address = "Boston"
                          //Department = null
            };
            Contact writer = new Contact()
            {
                Name       = "Kevin",
                Address    = "California",
                Department = itDepartment
            };
            Contact designer = new Contact()
            {
                Name       = "Ezra",
                Address    = "San Francisko",
                Department = itDepartment
            };
            ContactTask itContactTask = new ContactTask()
            {
                Contact = developer,
                Task    = itTask
            };
            ContactTask writeContactTask = new ContactTask()
            {
                Contact = writer,
                Task    = writeTask
            };
            ContactTask designContactTask = new ContactTask()
            {
                Contact = designer,
                Task    = designTask
            };

            dbContext.ContactTasks.Add(itContactTask);
            dbContext.ContactTasks.Add(designContactTask);
            dbContext.ContactTasks.Add(writeContactTask);
            dbContext.Tasks.Add(itTask);
            dbContext.Tasks.Add(writeTask);
            dbContext.Tasks.Add(designTask);
            dbContext.Contacts.Add(designer);
            dbContext.Contacts.Add(writer);
            dbContext.Contacts.Add(developer);
            dbContext.Departments.Add(itDepartment);
        }
        private static void CreateProductionDepartmentModel(EFCoreDemoDbContext dbContext)
        {
            Department productionDepartment = new Department()
            {
                Title  = "Production",
                Office = "Texas"
            };
            DemoTask packingTask = new DemoTask()
            {
                Description      = "Pack",
                Note             = "Packaging a products",
                StartDate        = new DateTime(1991, 09, 02),
                DateCompleted    = new DateTime(2016, 04, 12),
                PercentCompleted = 99
            };
            DemoTask transferTask = new DemoTask()
            {
                Description      = "Transfer",
                Note             = "Transfer a products to a customers",
                StartDate        = new DateTime(2008, 01, 13),
                DateCompleted    = new DateTime(2013, 02, 11),
                PercentCompleted = 100
            };
            DemoTask produceTask = new DemoTask()
            {
                Description      = "Produce",
                Note             = "Produce the finished product",
                StartDate        = new DateTime(2012, 12, 22),
                DateCompleted    = new DateTime(2017, 04, 01),
                PercentCompleted = 75
            };
            Contact packer = new Contact()
            {
                Name       = "Jack",
                Address    = "Minessota",
                Department = productionDepartment
            };
            Contact carrier = new Contact()
            {
                Name       = "Barry",
                Address    = "Chikago",
                Department = productionDepartment
            };
            Contact producer = new Contact()
            {
                Name       = "Mike",
                Address    = "London",
                Department = productionDepartment
            };
            ContactTask packingContactTask = new ContactTask()
            {
                Contact = packer,
                Task    = packingTask
            };
            ContactTask transferContactTask = new ContactTask()
            {
                Contact = carrier,
                Task    = transferTask
            };
            ContactTask produceContactTask = new ContactTask()
            {
                Contact = producer,
                Task    = produceTask
            };

            dbContext.Tasks.Add(packingTask);
            dbContext.Tasks.Add(transferTask);
            dbContext.Tasks.Add(produceTask);
            dbContext.ContactTasks.Add(packingContactTask);
            dbContext.ContactTasks.Add(transferContactTask);
            dbContext.ContactTasks.Add(produceContactTask);
            dbContext.Contacts.Add(packer);
            dbContext.Contacts.Add(carrier);
            dbContext.Contacts.Add(producer);
            dbContext.Departments.Add(productionDepartment);
        }