示例#1
0
 public ThingLog(User user, Thing thing)
 {
     this.Thing = thing;
     this.EditedBy = user;
     this.ThingId = thing.Id;
     this.EditedByUserId = user.Id;
     this.DateOccured = DateTime.Now;
 }
示例#2
0
        public HttpResponseMessage Post(ServiceModel.AddThingViewModel newThing)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
            }

            var thingCreator = context.GetAll <DomainModel.User>()
                               .FirstOrDefault(u => u.Id == newThing.CreatedById);

            if (thingCreator == null)
            {
                ModelState.AddModelError("", "Invalid Creator");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
            }

            var team = context.GetAll <DomainModel.Team>()
                       .FirstOrDefault(u => u.Id == newThing.TeamId);

            if (team == null)
            {
                ModelState.AddModelError("", "Invalid Team");
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
            }

            var thing = new DomainModel.Thing(team, thingCreator);

            thing.Description = newThing.Description;

            foreach (var userId in newThing.AssignedTo)
            {
                var assignedTo = context.GetAll <DomainModel.User>()
                                 .FirstOrDefault(u => u.Id == userId);

                if (assignedTo == null)
                {
                    ModelState.AddModelError("", "Invalid User Assigned to Thing");
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
                }

                thing.AssignedTo.Add(new DomainModel.UserThing(thing, assignedTo, thingCreator));
            }

            emailService.ThingAssigned(thing.AssignedTo.Select(x => x.AssignedToUser).ToArray(), thing, thingCreator).Send();

            context.Add(thing);
            context.SaveChanges();

            var sThing   = thing.MapToServiceModel();
            var response = Request.CreateResponse(HttpStatusCode.Created, sThing);

            response.Headers.Location = new Uri(Request.RequestUri, "/api/thing/" + thing.Id.ToString());
            return(response);
        }
示例#3
0
 public static ServiceModel.ThingBasic MapToBasicServiceModel(this DomainModel.Thing t)
 {
     if (t == null)
     {
         return(null);
     }
     return(new ServiceModel.ThingBasic()
     {
         Id = t.Id,
         Description = t.Description,
         Status = t.Status.ToString()
     });
 }
 public static ServiceModel.ThingBasic MapToBasicServiceModel(this DomainModel.Thing t)
 {
     if (t == null)
     {
         return(null);
     }
     return(new ServiceModel.ThingBasic()
     {
         Id = t.Id,
         Description = t.Description,
         Status = t.Status.ToString(),
         TeamId = t.TeamId,
         AssignedTo = t.AssignedTo.Select(a => a.AssignedToUser).Where(u => u.Id != t.OwnerId).MapToBasicServiceModel().ToList(),
         Owner = t.Owner.MapToBasicServiceModel()
     });
 }
示例#5
0
 public static ServiceModel.Thing MapToServiceModel(this DomainModel.Thing t)
 {
     if (t == null)
     {
         return(null);
     }
     return(new ServiceModel.Thing()
     {
         Id = t.Id,
         Description = t.Description,
         Status = t.Status.ToString(),
         DateCreated = t.DateCreated,
         AssignedTo = t.AssignedTo.Select(a => a.AssignedToUser).MapToBasicServiceModel(),
         Owner = t.Owner.MapToBasicServiceModel(),
         Team = t.Team.MapToBasicServiceModel()
     });
 }
示例#6
0
        private static void TestContext()
        {
            Console.WriteLine("Running Tests");

            User newUser;
            User newUser2;
            Team newTeam;
            Thing thing;
            using (var context = new TeamThingContext())
            {
                //add some new data
                newUser = new User("*****@*****.**");
                newUser.FirstName = "Josh";
                newUser.LastName = "Holt";
                context.Add(newUser);
                context.SaveChanges();

                //add some new data
                newUser2 = new User("*****@*****.**");
                newUser2.FirstName = "Josh2";
                newUser2.LastName = "Holt2";
                context.Add(newUser2);
                context.SaveChanges();

                newTeam = new Team("Closed Team", newUser, false);
                newTeam.TeamMembers.Add(new TeamUser(newTeam, newUser2));
                context.Add(newTeam);
                context.SaveChanges();

                thing = new Thing(newTeam, newUser);
                thing.Description = "Test Thing";
                thing.AssignedTo.Add(new UserThing(thing, newUser2, newUser));
                thing.AssignedTo.Add(new UserThing(thing, newUser, newUser));

                context.Add(thing);
                context.SaveChanges();
             }
            using (var context = new TeamThingContext())
            {
                newUser = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser.EmailAddress);
                newUser2 = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser2.EmailAddress);
                newTeam = context.GetAll<Team>().FirstOrDefault(p => p.Name == newTeam.Name);
                thing = context.GetAll<Thing>().FirstOrDefault(p => p.Description == thing.Description);
                ////recall the saved items
                //IProduct retrievedProduct = context.Products.FirstOrDefault(p => p.Name == newProduct.Name);
                //ICategory retrievedCategory = context.Categories.FirstOrDefault(p => p.Name == newCategory.Name);
                //Debug.Assert(retrievedProduct != null);
                //Debug.Assert(retrievedCategory!= null);
                //Debug.Assert(retrievedProduct.Category == retrievedCategory);
                //Debug.Assert(retrievedCategory.Products.Contains(retrievedProduct));
                Debug.Assert(newUser != null);
                Debug.Assert(newUser2 != null);
                Debug.Assert(newTeam != null);
                Debug.Assert(thing != null);

                Debug.Assert(thing.Owner != null);
                Debug.Assert(newTeam.Owner != null);

                Debug.Assert(newUser.Teams.Count > 0);

                Debug.Assert(newUser2.Teams.Count > 0);
                Debug.Assert(newUser2.Things.Count > 0);

                Debug.Assert(newTeam.TeamMembers.Count > 0);
                //Debug.Assert(newTeam.TeamThings.Count > 0);

                Debug.Assert(thing.AssignedTo.Count > 0);

            //        //clean up
            //        //Category is marked as dependent, so it will be removed automatically with the product
                context.Delete(thing);
                context.Delete(newTeam);
                context.Delete(newUser);
                context.Delete(newUser2);

                context.SaveChanges();
            }

            Console.WriteLine("All Tests Pass!");
        }
示例#7
0
        public HttpResponseMessage Post(ServiceModel.AddThingViewModel newThing)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
            }

            var thingCreator = context.GetAll<DomainModel.User>()
                                      .FirstOrDefault(u => u.Id == newThing.CreatedById);

            if (thingCreator == null)
            {
                ModelState.AddModelError("", "Invalid Creator");
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
            }

            var team = context.GetAll<DomainModel.Team>()
                              .FirstOrDefault(u => u.Id == newThing.TeamId);

            if (team == null)
            {
                ModelState.AddModelError("", "Invalid Team");
                return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
            }

            var thing = new DomainModel.Thing(team, thingCreator);
            thing.Description = newThing.Description;

            foreach (var userId in newThing.AssignedTo)
            {
                var assignedTo = context.GetAll<DomainModel.User>()
                                        .FirstOrDefault(u => u.Id == userId);

                if (assignedTo == null)
                {
                    ModelState.AddModelError("", "Invalid User Assigned to Thing");
                    return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson());
                }

                thing.AssignedTo.Add(new DomainModel.UserThing(thing, assignedTo, thingCreator));
            }

            emailService.ThingAssigned(thing.AssignedTo.Select(x => x.AssignedToUser).ToArray(), thing, thingCreator).Send();

            context.Add(thing);
            context.SaveChanges();

            var sThing = thing.MapToServiceModel();
            var response = Request.CreateResponse(HttpStatusCode.Created, sThing);
            response.Headers.Location = new Uri(Request.RequestUri, "/api/thing/" + thing.Id.ToString());
            return response;
        }
示例#8
0
        private static void TestContext()
        {
            Console.WriteLine("Running Tests");

            User newUser;
            User newUser2;
            Team newTeam;
            Thing thing;
            using (var context = new TeamThingContext())
            {
                //add some new data
                newUser = new User("UNIT TEST USER");
                newUser.FirstName = "Josh";
                newUser.LastName = "Holt";
                context.Add(newUser);
                context.SaveChanges();

                //add some new data
                newUser2 = new User("UNIT TEST USER 2");
                newUser2.FirstName = "Josh2";
                newUser2.LastName = "Holt2";
                context.Add(newUser2);
                context.SaveChanges();

                newTeam = new Team("UNIT TEST TEAM", newUser, false);
                newTeam.Members.Add(new TeamUser(newTeam, newUser2));
                context.Add(newTeam);
                context.SaveChanges();

                thing = new Thing(newTeam, newUser);
                thing.Description = "UNIT TEST THING";
                thing.AssignedTo.Add(new UserThing(thing, newUser2, newUser));
                thing.AssignedTo.Add(new UserThing(thing, newUser, newUser));

                context.Add(thing);
                context.SaveChanges();

                thing.UpdateStatus(newUser2, ThingStatus.Delayed);
                thing.Complete(newUser2);

                context.SaveChanges();
            }

            using (var context = new TeamThingContext())
            {
                newUser = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser.EmailAddress);
                newUser2 = context.GetAll<User>().FirstOrDefault(p => p.EmailAddress == newUser2.EmailAddress);
                newTeam = context.GetAll<Team>().FirstOrDefault(p => p.Name == newTeam.Name);
                thing = context.GetAll<Thing>().FirstOrDefault(p => p.Description == thing.Description);

                Debug.Assert(newUser != null);
                Debug.Assert(newUser2 != null);
                Debug.Assert(newTeam != null);
                Debug.Assert(thing != null);

                Debug.Assert(thing.Owner != null);
                Debug.Assert(newTeam.Owner != null);

                Debug.Assert(newUser.Teams.Count > 0);

                Debug.Assert(newUser2.Teams.Count > 0);
                Debug.Assert(newUser2.Things.Count > 0);

                Debug.Assert(newTeam.Members.Count > 0);
                //Debug.Assert(newTeam.TeamThings.Count > 0);

                Debug.Assert(thing.AssignedTo.Count > 0);

                Debug.Assert(thing.History.Count == 2);

                Debug.Assert(thing.History[0].EditedBy != null);
                Debug.Assert(thing.History[0].EditedByUserId != 0);

                Debug.Assert(thing.History[0].Thing == thing);
                Debug.Assert(thing.History[0].ThingId == thing.Id);

                //        //clean up
                //        //Category is marked as dependent, so it will be removed automatically with the product
                context.Delete(thing);
                context.Delete(newTeam);
                context.Delete(newUser);
                context.Delete(newUser2);

                context.SaveChanges();
            }

            Console.WriteLine("All Tests Pass!");
        }
示例#9
0
        public virtual MailMessage ThingUnassigned(User[] unassignedTo, Thing thing, User remover)
        {
            var mailMessage = new MailMessage { Subject = "TeamThing - Something off your plate!" };

            var sendTo = unassignedTo.Select(u => u.EmailAddress);

            if (sendTo.Count() > 0)
            {
                mailMessage.To.Add(string.Join(",", sendTo));
                mailMessage.From = new MailAddress(fromAddress);
                //ViewBag.Data = someObject;
                ViewData.Model = new ThingChanged { Thing = thing, ChangeMadeBy = remover };
                PopulateBody(mailMessage, viewName: "ThingUnassigned");

                return mailMessage;
            }

            return null;
        }
示例#10
0
        public virtual MailMessage ThingCompleted(User[] assignedTo, User completer, Thing thing)
        {
            var sendTo = assignedTo.Where(u => u.Id != completer.Id).Select(u => u.EmailAddress);

            if (sendTo.Count() > 0)
            {
                var mailMessage = new MailMessage { Subject = "TeamThing - Thing Completed" };
                mailMessage.To.Add(string.Join(",", sendTo));
                mailMessage.From = new MailAddress(fromAddress);
                //ViewBag.Data = someObject;
                ViewData.Model = new ThingChanged { Thing = thing, ChangeMadeBy = completer };
                PopulateBody(mailMessage, viewName: "ThingCompleted");

                return mailMessage;
            }

            return null;
        }