Example #1
0
 public IHttpActionResult DeleteOrganizationGTILink(int id)
 {
     try
     {
         UnitOfWork          unitOfWork = new UnitOfWork(factory);
         OrganizationGTILink link       = unitOfWork.OrganizationGTILinksRepository
                                          .Get(d => d.Id == id).FirstOrDefault();
         link.Deleted = true;
         unitOfWork.OrganizationGTILinksRepository.Update(link);
         unitOfWork.Save();
         return(Ok(link.ToDTO()));
     }
     catch (NotFoundException nfe)
     {
         return(NotFound());
     }
     catch (ConflictException ce)
     {
         return(Conflict());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Example #2
0
        public void DeleteLink_ShouldDeleteAndReturnOk()
        {
            var linksTestData = new List <OrganizationGTILink>()
            {
                new OrganizationGTILink {
                    Id = 1, OrganizationId = 2
                },
                new OrganizationGTILink {
                    Id = 2, Deleted = true, OrganizationId = 2
                },
                new OrganizationGTILink {
                    Id = 3, OrganizationId = 3
                }
            };
            var links          = MockHelper.MockDbSet(linksTestData);
            var orggtiTestData = new List <OrganizationGTI>()
            {
                new OrganizationGTI {
                    Id = 1
                }
            };
            var orgs        = MockHelper.MockDbSet(orggtiTestData);
            var offTestData = new List <Office>()
            {
                new Office {
                    Id = 1
                }
            };
            var offices   = MockHelper.MockDbSet(offTestData);
            var dbContext = new Mock <IAppDbContext>();

            dbContext.Setup(m => m.OrganizationGTILinks).Returns(links.Object);
            dbContext.Setup(d => d.Set <OrganizationGTILink>()).Returns(links.Object);

            dbContext.Setup(m => m.GTIOrganizations).Returns(orgs.Object);
            dbContext.Setup(d => d.Set <OrganizationGTI>()).Returns(orgs.Object);

            dbContext.Setup(m => m.Offices).Returns(offices.Object);
            dbContext.Setup(d => d.Set <Office>()).Returns(offices.Object);

            links.Setup(d => d.Find(It.IsAny <object>())).Returns <object[]>((keyValues) => { return(links.Object.SingleOrDefault(product => product.Id == (int)keyValues.Single())); });

            var factory = new Mock <IDbContextFactory>();

            factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object);

            OrganizationGTILink link = new OrganizationGTILink {
                Id = 3, OrganizationId = 3
            };
            var controller = new OrganizationGTILinksController(factory.Object);
            var result     = controller.DeleteOrganizationGTILink(3) as OkNegotiatedContentResult <OrganizationGTILinkDTO>;

            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Content.Id);
            Assert.AreEqual(3, result.Content.OrganizationId);
        }
Example #3
0
        public void PostLink_ShoulAddLink()
        {
            var linksTestData = new List <OrganizationGTILink>()
            {
                new OrganizationGTILink {
                    Id = 1, OrganizationId = 2
                },
                new OrganizationGTILink {
                    Id = 2, Deleted = true, OrganizationId = 2
                },
                new OrganizationGTILink {
                    Id = 3, OrganizationId = 3
                }
            };
            var links          = MockHelper.MockDbSet(linksTestData);
            var orggtiTestData = new List <OrganizationGTI>()
            {
                new OrganizationGTI {
                    Id = 77, OfficeId = 1, NativeName = "Naive name of organization"
                }
            };
            var orgs        = MockHelper.MockDbSet(orggtiTestData);
            var offTestData = new List <Office>()
            {
                new Office {
                    Id = 1, ShortName = "Some short office name"
                }
            };
            var offices   = MockHelper.MockDbSet(offTestData);
            var dbContext = new Mock <IAppDbContext>();

            dbContext.Setup(m => m.OrganizationGTILinks).Returns(links.Object);
            dbContext.Setup(d => d.Set <OrganizationGTILink>()).Returns(links.Object);

            dbContext.Setup(m => m.GTIOrganizations).Returns(orgs.Object);
            dbContext.Setup(d => d.Set <OrganizationGTI>()).Returns(orgs.Object);

            dbContext.Setup(m => m.Offices).Returns(offices.Object);
            dbContext.Setup(d => d.Set <Office>()).Returns(offices.Object);

            links.Setup(d => d.Find(It.IsAny <object>())).Returns <object[]>((keyValues) => { return(links.Object.SingleOrDefault(product => product.Id == (int)keyValues.Single())); });
            links.Setup(d => d.Add(It.IsAny <OrganizationGTILink>())).Returns <OrganizationGTILink>((contact) =>
            {
                linksTestData.Add(contact);
                links = MockHelper.MockDbSet(linksTestData);
                return(contact);
            });


            dbContext.Setup(d => d.ExecuteStoredProcedure <int>(It.IsAny <string>(), It.IsAny <object[]>()))
            .Returns <string, object[]>((query, parameters) =>
            {
                List <int> list = new List <int>();
                if (query.Contains("NewTableId"))
                {
                    int i = links.Object.Max(d => d.Id) + 1;
                    list.Add(i);
                }
                else
                {
                    list.Add(0);
                }
                return(list);
            });

            var factory = new Mock <IDbContextFactory>();

            factory.Setup(m => m.CreateDbContext()).Returns(dbContext.Object);



            OrganizationGTILink link = new OrganizationGTILink {
                Id = 33, OrganizationId = 55, GTIId = 77
            };


            var identityHelper = new Mock <IIdentityHelper>();
            // identityHelper.Setup(d => d.GetUserTableName(It.IsAny<IPrincipal>())).Returns("Employee");
            // identityHelper.Setup(d => d.GetUserTableId(It.IsAny<IPrincipal>())).Returns(11);



            var controller = new OrganizationGTILinksController(factory.Object, identityHelper.Object);

            var result = controller.PostOrganizationGTILink(link.ToDTO()) as CreatedAtRouteNegotiatedContentResult <OrganizationGTILinkDTO>;

            Assert.IsNotNull(result);
            Assert.AreEqual(4, result.Content.Id);
            Assert.AreEqual(55, result.Content.OrganizationId);
            Assert.AreEqual(77, result.Content.GTIId);
        }
Example #4
0
        public IHttpActionResult PostOrganizationGTICreateLink(OrganizationGTICreateLinkDTO links)
        {
            if (links == null)
            {
                return(BadRequest(ModelState));
            }
            if (links.OrganizationGTIIds == null)
            {
                return(BadRequest());
            }
            // string userId = ActionContext.RequestContext.Principal.Identity.GetUserId();
            //  ApplicationUser user = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(userId);
            //  ApplicationUser user = identityHelper.FindUserById(User.Identity.GetUserId());
            if (identityHelper.GetUserTableName(User) == "Employee")
            {
                UnitOfWork unitOfWork = new UnitOfWork(factory);
                foreach (var item in links.OrganizationGTIIds)
                {
                    OrganizationGTILink existingLink = unitOfWork.OrganizationGTILinksRepository
                                                       .Get(d => d.Deleted != true && d.GTIId == item)
                                                       .FirstOrDefault();
                    if (existingLink != null)
                    {
                        return(BadRequest(String.Format("Link to {0} already exist", unitOfWork.OrganizationGTIsRepository.GetByID(item).NativeName)));
                    }
                }
                List <OrganizationGTILinkDTO> createdLinks = new List <OrganizationGTILinkDTO>();
                int EmployeeId    = identityHelper.GetUserTableId(User);
                int OrganzationId = links.OrganizationId;
                try
                {
                    foreach (var item in links.OrganizationGTIIds)
                    {
                        //Добавление
                        int OrganizationGTIId    = item;
                        OrganizationGTILink link = new OrganizationGTILink();
                        link.Id             = link.NewId(unitOfWork);
                        link.OrganizationId = OrganzationId;
                        link.GTIId          = OrganizationGTIId;
                        link.EmployeeId     = EmployeeId;
                        unitOfWork.OrganizationGTILinksRepository.Insert(link);
                        unitOfWork.Save();
                        //Достать со всеми вложенными
                        OrganizationGTILinkDTO newLink = unitOfWork.OrganizationGTILinksRepository
                                                         .Get(d => d.Id == link.Id).FirstOrDefault().ToDTO();
                        if (link.GTIId != null)
                        {
                            newLink.OrganizationGTI = unitOfWork.OrganizationGTIsRepository
                                                      .Get(d => d.Id == newLink.GTIId).FirstOrDefault().ToDTO();

                            if (newLink.OrganizationGTI != null)
                            {
                                newLink.OrganizationGTI.Office = unitOfWork.OfficesRepository
                                                                 .Get(d => d.Id == newLink.OrganizationGTI.OfficeId).FirstOrDefault().ToDTO();
                            }
                        }
                        createdLinks.Add(newLink);
                    }
                }
                catch (NotFoundException nfe)
                {
                    return(NotFound());
                }
                catch (ConflictException ce)
                {
                    return(Conflict());
                }
                catch (Exception e)
                {
                    return(BadRequest(e.Message));
                }

                return(Ok(createdLinks));
            }
            return(BadRequest());
        }
Example #5
0
        public IHttpActionResult PostOrganizationGTILink(OrganizationGTILinkDTO organizationGTILink)
        {
            try
            {
                if (organizationGTILink == null)
                {
                    return(BadRequest(ModelState));
                }

                if (identityHelper.GetUserTableName(User) == "Employee")
                {
                    OrganizationGTILink link = organizationGTILink.FromDTO();

                    int EmployeeId = identityHelper.GetUserTableId(User);
                    link.EmployeeId = EmployeeId;



                    UnitOfWork          unitOfWork   = new UnitOfWork(factory);
                    OrganizationGTILink existingLink = unitOfWork.OrganizationGTILinksRepository
                                                       .Get(d => d.Deleted != true && d.GTIId == link.GTIId)
                                                       .FirstOrDefault();
                    if (existingLink != null)
                    {
                        return(BadRequest("Link to this GTI Organization already exist"));
                    }



                    link.Id = link.NewId(unitOfWork);
                    unitOfWork.OrganizationGTILinksRepository.Insert(link);
                    unitOfWork.Save();



                    OrganizationGTILinkDTO dto = unitOfWork.OrganizationGTILinksRepository
                                                 .Get(d => d.Id == link.Id).FirstOrDefault().ToDTO();

                    if (dto.GTIId != null)
                    {
                        dto.OrganizationGTI = unitOfWork.OrganizationGTIsRepository
                                              .Get(d => d.Id == dto.GTIId).FirstOrDefault().ToDTO();
                        if (dto.OrganizationGTI != null)
                        {
                            dto.OrganizationGTI.Office = unitOfWork.OfficesRepository
                                                         .Get(d => d.Id == dto.OrganizationGTI.OfficeId).FirstOrDefault().ToDTO();
                        }
                    }
                    return(CreatedAtRoute("GetOrganizationGTILink", new { id = dto.Id }, dto));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (NotFoundException nfe)
            {
                return(NotFound());
            }
            catch (ConflictException ce)
            {
                return(Conflict());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }