コード例 #1
0
 public static SubmittedTask GetTaskById(int id)
 {
     using (var context = new CodeKataContext())
     {
         return(context.SubmittedTasks.Single(tsk => tsk.Id == id));
     }
 }
コード例 #2
0
 public void UpdateExistingTask()
 {
     using (var context = new CodeKataContext())
     {
         // https://msdn.microsoft.com/en-us/data/jj592676
         context.Entry(this).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
コード例 #3
0
        // List
        //public virtual ICollection<TestModel2> TestModel2s { get; set; }


        // WinService Methods
        public static List <SubmittedTask> GetTasksByStatus(TaskStatus taskStatus)
        {
            using (var context = new CodeKataContext())
            {
                return(context.SubmittedTasks
                       .Include("LastUpdatedBy")
                       .Include("SubmittedBy")
                       .Where(tsk => tsk.Status == taskStatus)
                       .ToList());
            }
        }
コード例 #4
0
        public static void Seed(CodeKataContext context, Faker <User> testUsers)
        {
            Randomizer.Seed = new Random(3897234);

            // CONFIG
            var numSubmittedTasksToGen = 500;

            var recentDay = DateTime.Now.AddDays(-2);

            // SUBMITTEDTASK RULES
            var testSubmittedTasks = new Faker <SubmittedTask>()
                                     .RuleFor(u => u.Name, f => f.Lorem.Sentence(3))
                                     .RuleFor(u => u.Description, f => f.Lorem.Sentence())
                                     .RuleFor(u => u.Type, f => f.PickRandom <TaskType>())
                                     .RuleFor(u => u.Status, f => f.PickRandom <TaskStatus>())
                                     .RuleFor(u => u.SubmitDateTime, f => f.Date.Recent(2))
                                     .RuleFor(u => u.SubmittedBy, f => testUsers.Generate())
                                     .RuleFor(u => u.StartDateTime, (f, u) => (u.Status != TaskStatus.Queued) ? f.Date.Between(recentDay.AddMinutes(55), recentDay).AddMinutes(120) : SqlDateTime.MinValue.Value)
                                     .RuleFor(u => u.EndDateTime, (f, u) => (u.Status == TaskStatus.Error || u.Status == TaskStatus.Finished) ? f.Date.Between(recentDay.AddMinutes(120), recentDay.AddMinutes(600)) : SqlDateTime.MinValue.Value)
                                     .RuleFor(u => u.LastUpdatedDateTime, (f, u) => u.EndDateTime)
                                     .RuleFor(u => u.LastUpdatedBy, (f, u) => u.SubmittedBy)
                                     .FinishWith((f, u) =>
            {
                Console.WriteLine("Submitted Task Created! Id={0}", u.Id);
            });

            var cachedUser = testUsers.Generate();

            while (numSubmittedTasksToGen > 0)
            {
                var genNewUser = new Randomizer().Number(0, 10) > 3;
                var newTask    = testSubmittedTasks.Generate();

                // > 1 job per user
                cachedUser            = genNewUser ? newTask.SubmittedBy : cachedUser;
                newTask.SubmittedBy   = cachedUser;
                newTask.LastUpdatedBy = cachedUser;

                // todo: check statuses match timestamps (queued should not have a finishedDateTime)

                if (newTask.Status != TaskStatus.Processing)
                {
                    context.SubmittedTasks.Add(newTask);
                }
                numSubmittedTasksToGen--;
            }
        }
コード例 #5
0
        public static Faker <User> Seed(CodeKataContext context)
        {
            Randomizer.Seed = new Random(3897234);

            var testUsers = new Faker <User>()
                            .RuleFor(u => u.EmployeeId, f => f.Random.Replace("######"))
                            .RuleFor(u => u.FirstName, f => f.Name.FirstName())
                            .RuleFor(u => u.LastName, f => f.Name.LastName())
                            .RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName, "gohomeside.com"))
                            .RuleFor(u => u.Phone, f => f.Random.Replace("##########"))
                            .FinishWith((f, u) =>
            {
                Console.WriteLine("User Created! Id={0}", u.Id);
            });

            return(testUsers);
        }
コード例 #6
0
        public ActionResult GetFile(int fileId)
        {
            try
            {
                Attachment matchedFile;
                using (var context = new CodeKataContext())
                {
                    matchedFile = context.Attachments.Single(att => att.Id == fileId);
                }

                var memoryStream = new MemoryStream(matchedFile.FileContent);
                return(new FileStreamResult(memoryStream, matchedFile.FileType)
                {
                    FileDownloadName = matchedFile.FileName
                });
            }
            catch (Exception ex)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request"));
            }
        }
コード例 #7
0
        public ActionResult SubmitTask(HttpPostedFileBase fileToUpload, SubmittedTaskFormDto submittedTaskForm)
        {
            var fileExists = fileToUpload != null && fileToUpload.ContentLength > 0;

            if (!fileExists)
            {
                return(new JsonNetResult
                {
                    Data = new Notification(400, "Bad Request! Upload Failed")
                });
            }

            // todo: combine these into a single map? Or..
            // todo: Easier to follow logic when separate?
            var newAttachment = _mapper.Map <Attachment>(fileToUpload);
            var newTask       = _mapper.Map <SubmittedTask>(submittedTaskForm);

            newTask.Attachment = newAttachment;

            using (var context = new CodeKataContext())
            {
                // Associate with user
                var matchedUser = context.Users.Single(usr => usr.Id == submittedTaskForm.SubmittedById);
                newTask.SubmittedBy   = matchedUser;
                newTask.LastUpdatedBy = matchedUser;

                // Tell EF that this is an existing obj
                context.Users.Attach(newTask.SubmittedBy);
                context.Users.Attach(newTask.LastUpdatedBy);

                // Add & Update
                context.SubmittedTasks.Add(newTask);
                context.SaveChanges();
            }

            return(new JsonNetResult
            {
                Data = new Notification(200, "Task submitted successfully!")
            });
        }
コード例 #8
0
        public ActionResult GetUsers(string searchTerm = "", int page = 1)
        {
            List <User> allUsers;

            using (var context = new CodeKataContext())
            {
                allUsers = context.Users
                           .Where(usr => (usr.FirstName + " " + usr.LastName + " " + usr.EmployeeId).Contains(searchTerm))
                           .OrderBy(usr => usr.FirstName)
                           .ToList();
            }

            var returnDictionary = new Dictionary <string, dynamic>
            {
                { "items", _mapper.Map <List <User>, List <UserSearchDto> >(allUsers) },
                { "total_count", allUsers.Count() }
            };

            return(new JsonNetResult {
                Data = returnDictionary
            });
        }
コード例 #9
0
        public ActionResult SubmittedTasks()
        {
            List <SubmittedTask> allSubmittedTasks;

            using (var context = new CodeKataContext())
            {
                allSubmittedTasks = context.SubmittedTasks
                                    .Include("Attachment")
                                    .Include("LastUpdatedBy")
                                    .Include("SubmittedBy")
                                    .ToList();
            }

            var returnDictionary = new Dictionary <string, List <SubmittedTaskDto> >
            {
                { "data", _mapper.Map <List <SubmittedTask>, List <SubmittedTaskDto> >(allSubmittedTasks) }
            };

            return(new JsonNetResult {
                Data = returnDictionary
            });
        }