예제 #1
0
        public void UpdateDeveloperProject(string UserID, int?ProjectID)
        {
            var developer = db.BugTrackerUsers.FirstOrDefault(u => u.BugTrackerUserID == UserID);

            developer.ProjectID = ProjectID;
            db.SaveChanges();
        }
        public IHttpActionResult PatchBug(int id, Bug bugData)
        {
            if (bugData == null)
            {
                return(this.BadRequest());
            }

            var bug = db.Bugs.Find(id);

            if (bug == null)
            {
                return(this.NotFound());
            }

            if (bugData.Title != null)
            {
                bug.Title = bugData.Title;
            }

            if (bugData.Description != null)
            {
                bug.Description = bugData.Description;
            }

            if (bugData.Status != bug.Status)
            {
                bug.Status = bugData.Status;
            }

            db.SaveChanges();
            return(this.Ok(new { Message = "Bug #" + bug.Id + " patched." }));
        }
예제 #3
0
        public void AddBug_WhenBugIsValid_ShouldAddToDb()
        {
            // Arrange -> prepare the objects
            var bug = new Bug()
            {
                Text        = "Sample New Bug",
                DateCreated = DateTime.Now,
                Status      = BugStatus.New
            };

            var dbContext = new BugTrackerDbContext();

            // Act -> perform some logic
            dbContext.Bugs.Add(bug);
            dbContext.SaveChanges();

            // Assert -> validate the results
            var bugInDb = dbContext.Bugs.Find(bug.Id);

            Assert.IsNotNull(bugInDb);
            Assert.AreEqual(bug.Text, bugInDb.Text);
            Assert.AreEqual(bug.Status, bugInDb.Status);
            Assert.AreEqual(bug.DateCreated, bugInDb.DateCreated);
            Assert.IsTrue(bugInDb.Id != 0);
        }
예제 #4
0
        public IHttpActionResult PostComment(int id, Comment commentData)
        {
            if (commentData == null)
            {
                return(this.BadRequest());
            }

            var bug = db.Bugs.Find(id);

            if (bug == null)
            {
                return(this.NotFound());
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = db.Users.Find(currentUserId);

            var comment = new Comment
            {
                Text        = commentData.Text,
                DateCreated = DateTime.Now,
                Author      = currentUser,
                Bug         = bug
            };

            db.Comments.Add(comment);
            db.SaveChanges();

            if (comment.Author == null)
            {
                return(this.Ok(new { Id = id, Message = "Added anonymous comment for bug #" + bug.Id }));
            }

            return(this.Ok(new { Id = id, Author = comment.Author.UserName, Message = "User comment added for bug #" + bug.Id }));
        }
 public static void CleanDatabase()
 {
     using (var dbContext = new BugTrackerDbContext())
     {
         dbContext.Comments.Delete();
         dbContext.Bugs.Delete();
         dbContext.Users.Delete();
         dbContext.SaveChanges();
     }
 }
예제 #6
0
        public void ListBugs_NonEmptyDb_ShouldReturnBugsList()
        {
            // Arrange
            CleanDatabase();

            dbContext.Bugs.Add(
                new Bug()
            {
                Text        = "Bug #" + DateTime.Now.Ticks,
                DateCreated = DateTime.Now,
                Status      = BugStatus.New
            });
            dbContext.Bugs.Add(
                new Bug()
            {
                Text        = "Bug #" + DateTime.Now.Ticks,
                DateCreated = DateTime.Now,
                Status      = BugStatus.Fixed
            });

            dbContext.SaveChanges();

            // Act
            var httpResponse    = httpClient.GetAsync("/api/bugs").Result;
            var bugsFromService = httpResponse.Content.ReadAsAsync <List <Bug> >().Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);
            Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json");

            var bugsFromDb = dbContext.Bugs.ToList();

            Assert.AreEqual(bugsFromDb.Count, bugsFromService.Count);

            // Assert the bugs in the DB are the same as the bugs returned from the service
            for (int i = 0; i < bugsFromService.Count; i++)
            {
                Assert.AreEqual(bugsFromService[i].Id, bugsFromDb[i].Id);
                Assert.AreEqual(bugsFromService[i].Text, bugsFromDb[i].Text);
                Assert.AreEqual(bugsFromService[i].Status, bugsFromDb[i].Status);
                Assert.AreEqual(bugsFromService[i].DateCreated.ToString(), bugsFromDb[i].DateCreated.ToString());
            }
        }
예제 #7
0
        public IHttpActionResult CreateBug(BugsBindingModel bugData)
        {
            if (bugData == null)
            {
                return(BadRequest("Missing bug data."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            User user = null;

            if (User.Identity.IsAuthenticated)
            {
                user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);
            }

            var bug = new Bug()
            {
                Title       = bugData.Title,
                Description = bugData.Description,
                Status      = BugStatus.Open,
                Author      = user,
                DateCreated = DateTime.Now
            };

            db.Bugs.Add(bug);
            db.SaveChanges();
            if (user != null)
            {
                return(this.CreatedAtRoute(
                           "DefaultApi",
                           new { controller = "bugs", id = bug.Id },
                           new { Id = bug.Id, Author = user.UserName, Message = "User bug submitted." }));
            }
            return(this.CreatedAtRoute(
                       "DefaultApi",
                       new { controller = "bugs", id = bug.Id },
                       new { Id = bug.Id, Message = "Anonymous bug submitted." }));
        }
예제 #8
0
        private void CleanDatabase()
        {
            // Clean all data in all database tables
            var dbContext = new BugTrackerDbContext();

            dbContext.Bugs.Delete();
            // ...
            // dbContext.AnotherEntity.Delete();
            // ...
            dbContext.SaveChanges();
        }
예제 #9
0
        public void AddNotificationToUser(string message, string link, string userId)
        {
            Notification addedNotification = new Notification
            {
                Message = message,
                Link    = link,
                UserID  = userId
            };

            db.Notifications.Add(addedNotification);
            db.SaveChanges();
        }
예제 #10
0
        public void AddBug_WhenBugIsInvalid_ShouldThrowException()
        {
            // Arrange -> prapare the objects
            var dbContext  = new BugTrackerDbContext();
            var invalidBug = new Bug()
            {
                Text = null
            };

            // Act -> perform some logic
            dbContext.Bugs.Add(invalidBug);
            dbContext.SaveChanges();

            // Assert -> expect an exception
        }
예제 #11
0
        public IHttpActionResult AddCommentForGivenBug(int id, AddCommentBindingModel addCommentModel)
        {
            if (addCommentModel == null)
            {
                return(this.BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var bug = db.Bugs.FirstOrDefault(b => b.Id == id);

            if (bug == null)
            {
                return(this.NotFound());
            }

            var currentUserId = User.Identity.GetUserId();
            var author        = db.Users.FirstOrDefault(u => u.Id == currentUserId);

            Comment comment = new Comment()
            {
                Text        = addCommentModel.Text,
                PublishDate = DateTime.Now,
                Author      = author
            };

            bug.Comments.Add(comment);
            db.SaveChanges();

            if (author == null)
            {
                return(this.Ok(new
                {
                    Id = comment.Id,
                    Message = "Added anonymous comment for bug #" + bug.Id
                }));
            }

            return(this.Ok(new
            {
                Id = comment.Id,
                Author = author.UserName,
                Message = "User comment added for bug #" + bug.Id
            }));
        }
예제 #12
0
        public IActionResult SignUpRedirect(string state, BugTrackerUser _User)
        {
            if (ModelState.IsValid)
            {
                db.BugTrackerUsers.Add(_User);

                db.SaveChanges();

                rolesManipulator.AssignRoleToUser(_User.BugTrackerUserID, RolesIDs.SubmitterID);

                return(Redirect($"https://dev-5rw-rtkk.eu.auth0.com/continue?state={state}"));
            }

            ViewBag.state = state;
            return(View(_User));
        }
예제 #13
0
        public IHttpActionResult CreateComment(int id, CommentBindingModel commentData)
        {
            var bug = db.Bugs.Find(id);

            if (bug == null)
            {
                return(NotFound());
            }

            if (commentData == null)
            {
                return(BadRequest("Missing comment data."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            User user = null;

            if (User.Identity.IsAuthenticated)
            {
                user = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);
            }

            var comment = new Comment()
            {
                Text        = commentData.Text,
                Author      = user,
                PublishDate = DateTime.Now,
                Bug         = bug
            };

            db.Comments.Add(comment);
            db.SaveChanges();

            if (user != null)
            {
                return(this.Ok(
                           new { Id = comment.Id, Author = user.UserName, Message = "User comment added for bug #" + bug.Id }));
            }

            return(this.Ok(
                       new { Id = comment.Id, Message = "Added anonymous comment for bug #" + bug.Id }));
        }
        public void FindWhenObjectIsInDbShouldReturnObject()
        {
            // Arrange
            var bug = GetValidTestBug();

            var databaseContext = new BugTrackerDbContext();
            var repo            = new EfRepository <Bug>(databaseContext);

            // Act
            databaseContext.Bugs.Add(bug);
            databaseContext.SaveChanges();

            var bugInDb = repo.Find(bug.Id);

            // Assert
            Assert.IsNotNull(bugInDb);
            Assert.AreEqual(bug.Text, bugInDb.Text);
        }
예제 #15
0
        public IHttpActionResult SubmitNewBug(BugBindingModel bugModel)
        {
            if (bugModel.Title == null)
            {
                return(BadRequest("Invalid bug title"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId = User.Identity.GetUserId();
            var user          = db.Users.FirstOrDefault(u => u.Id == currentUserId);

            var bug = new Bug()
            {
                Title       = bugModel.Title,
                Description = bugModel.Description,
                Status      = Status.Open,
                SubmitDate  = DateTime.Now,
                Author      = user != null ? user : null
            };

            db.Bugs.Add(bug);
            db.SaveChanges();

            var bugOutput = new BugViewModel()
            {
                Id          = bug.Id,
                Author      = bug.Author != null ? bug.Author.UserName : null,
                Status      = bug.Status.ToString(),
                Title       = bug.Title,
                DateCreated = bug.SubmitDate
            };

            if (currentUserId != null)
            {
                bugOutput.Author = bug.Author.UserName;
                return(CreatedAtRoute(
                           "DefaultApi",
                           new
                {
                    id = bugOutput.Id,
                    Author = bugOutput.Author,
                    Message = "User bug submitted."
                },
                           new
                {
                    id = bugOutput.Id,
                    Author = bugOutput.Author,
                    Message = "User bug submitted."
                }));
            }

            return(CreatedAtRoute(
                       "DefaultApi",
                       new
            {
                id = bugOutput.Id,
                Message = "Anonymous bug submitted."
            },
                       new
            {
                id = bugOutput.Id,
                Message = "Anonymous bug submitted."
            }));
        }
예제 #16
0
 public void AddTicket(Ticket ticket)
 {
     db.Add(ticket);
     db.SaveChanges();
 }