Пример #1
0
        public Task GetTask(string id)
        {
            int parsedId = ParseTaskId(id);

            using (TeamTaskObjectContext objectContext = new TeamTaskObjectContext())
            {
                var task = objectContext.Tasks.FirstOrDefault(t => t.Id == parsedId);
                if (task == null)
                {
                    ThrowNoSuchTaskId(parsedId);
                }
                return(task);
            }
        }
 public User GetUser(string userName)
 {
     using (TeamTaskObjectContext objectContext = new TeamTaskObjectContext())
     {
         // The 'Include' method is an ADO.NET Entity Framework feature that allows
         //  us to load the user and his/her tasks in a single query!
         var user = objectContext.Users.Include("Tasks").FirstOrDefault(u => u.UserName == userName);
         if (user == null)
         {
             throw new WebFaultException <string>(
                       string.Format("There is no user with the userName '{0}'.", userName),
                       HttpStatusCode.NotFound);
         }
         return(user);
     }
 }
        public List <User> GetUsers(int skip, int top, string userName)
        {
            // Set reasonable defaults for the query string parameters
            skip = (skip >= 0) ? skip : 0;
            top  = (top > 0) ? top : 25;

            using (TeamTaskObjectContext objectContext = new TeamTaskObjectContext())
            {
                // Include the where clause only if a userName was provided
                var userQuery = (string.IsNullOrWhiteSpace(userName)) ?
                                objectContext.Users :
                                objectContext.Users.Where(user => user.ManagerUserName == userName);

                return(userQuery.OrderBy(user => user.UserName).Skip(skip).Take(top).ToList());
            }
        }
Пример #4
0
        public List <Task> GetTasks(int skip, int top, string userName, string format)
        {
            // Set reasonable defaults for the query string parameters
            skip = (skip >= 0) ? skip : 0;
            top  = (top > 0) ? top : 25;

            // Set the format from the format query variable if it is available
            if (string.Equals("json", format, StringComparison.OrdinalIgnoreCase))
            {
                WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
            }

            using (TeamTaskObjectContext objectContext = new TeamTaskObjectContext())
            {
                // Include the where clause only if a userName was provided
                var taskQuery = (string.IsNullOrWhiteSpace(userName)) ?
                                objectContext.Tasks :
                                objectContext.Tasks.Where(task => task.OwnerUserName == userName);

                return(taskQuery.OrderBy(task => task.Id).Skip(skip).Take(top).ToList());
            }
        }
Пример #5
0
        public Task UpdateTask(string id, Task task)
        {
            // The id from the URI determines the id of the task
            int parsedId = ParseTaskId(id);

            task.Id = parsedId;

            using (TeamTaskObjectContext objectContext = new TeamTaskObjectContext())
            {
                objectContext.AttachTask(task);
                try
                {
                    objectContext.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    ThrowNoSuchTaskId(parsedId);
                }
                // Because we allow partial updates, we need to refresh the task from the dB
                objectContext.Refresh(RefreshMode.StoreWins, task);
            }

            return(task);
        }