public void GetTagShouldReturnCorrectly()
        {
            var tagForExpert = new Tag {
                Name = "expertTag"
            };
            var expertTag = new ExpertTag {
                Tag = tagForExpert
            };
            var tagForMainField = new Tag {
                Name = "mainFieldTag"
            };
            var mainFieldTag = new MainFieldTag {
                Tag = tagForMainField
            };
            var review = new Review {
                Name = "review",
            };
            var expert = new Expert
            {
                ExpertCategory = new Category {
                    Name = "category"
                },
                ExpertTags = new List <ExpertTag> {
                    expertTag
                },
                MainFields = new List <MainFieldTag> {
                    mainFieldTag
                },
                Reviews = new List <Review> {
                    review
                }
            };

            var experts = new List <Expert> {
                expert
            };

            _unitOfWorkMock.Setup(x =>
                                  x.Experts.GetExpertsWithTagName(It.IsAny <string>()))
            .Returns(experts.AsQueryable);

            var actResult      = _uut.GetAllExpertsWithTag("dummy");
            var returnedExpert = actResult.Value[0];

            Assert.AreEqual(returnedExpert.ExpertCategory,
                            expert.ExpertCategory.Name);
            Assert.AreEqual(returnedExpert.ExpertTags.ElementAt(0),
                            expert.ExpertTags.ElementAt(0).Tag.Name);
            Assert.AreEqual(returnedExpert.MainFields.ElementAt(0),
                            expert.MainFields.ElementAt(0).Tag.Name);
            Assert.AreEqual(returnedExpert.Reviews.ElementAt(0).Name,
                            expert.Reviews.ElementAt(0).Name);
        }
Example #2
0
        public async Task <IActionResult> Register([FromBody] DTORegisterUser dtoUser)
        {
            using (var unit = _factory.GetUOF())
            {
                try
                {
                    // Add User
                    IdentityResult userCreationResult;
                    if (dtoUser.IsExpert)
                    {
                        var newUser = new Expert
                        {
                            UserName    = dtoUser.Email,
                            Email       = dtoUser.Email,
                            City        = dtoUser.City,
                            Birthday    = dtoUser.Birthday,
                            FirstName   = dtoUser.FirstName,
                            LastName    = dtoUser.LastName,
                            Gender      = dtoUser.Gender,
                            IsExpert    = dtoUser.IsExpert,
                            PhoneNumber = dtoUser.PhoneNumber,
                            Calendar    = new Calendar(),
                            Description = dtoUser.Description,
                            ExpertTags  = new List <ExpertTag>(),
                            MainFields  = new List <MainFieldTag>(),
                            Reviews     = new List <Review>()
                        };
                        userCreationResult = await _userManager.CreateAsync(newUser, dtoUser.Password);
                    }
                    else
                    {
                        var newUser = new User
                        {
                            UserName    = dtoUser.Email,
                            Email       = dtoUser.Email,
                            City        = dtoUser.City,
                            Birthday    = dtoUser.Birthday,
                            FirstName   = dtoUser.FirstName,
                            LastName    = dtoUser.LastName,
                            Gender      = dtoUser.Gender,
                            IsExpert    = dtoUser.IsExpert,
                            PhoneNumber = dtoUser.PhoneNumber,
                            Calendar    = new Calendar()
                        };
                        userCreationResult = await _userManager.CreateAsync(newUser, dtoUser.Password);
                    }


                    // Add user tag
                    if (userCreationResult.Succeeded)
                    {
                        var dbUser = unit.Users.GetByEmail(dtoUser.Email);
                        dbUser.InterestTags = new Collection <UserTag>();

                        if (dtoUser.InterestTags == null)
                        {
                            dtoUser.InterestTags = new Collection <string>();
                        }

                        foreach (var interestTag in dtoUser.InterestTags)
                        {
                            var tag = unit.Tags.GetTagByName(interestTag) ?? new Tag {
                                Name = interestTag
                            };

                            var userTag = new UserTag
                            {
                                Tag  = tag,
                                User = dbUser
                            };

                            dbUser.InterestTags.Add(userTag);
                        }

                        unit.Complete();

                        if (dtoUser.IsExpert)
                        {
                            var dbExpert = unit.Experts.GetByEmail(dtoUser.Email);
                            dbExpert.ExpertTags = new List <ExpertTag>();
                            dbExpert.MainFields = new List <MainFieldTag>();
                            var category = unit.Categories.GetCategoryEagerByName(dtoUser.ExpertCategory);

                            if (dtoUser.ExpertTags == null)
                            {
                                return(BadRequest(new { message = "You must have some expert tags" }));
                            }

                            foreach (var expertTag in dtoUser.ExpertTags)
                            {
                                var tag = unit.Tags.GetTagByName(expertTag) ?? new Tag {
                                    Name = expertTag
                                };

                                category.Tags.Add(tag);

                                var newExpertTag = new ExpertTag
                                {
                                    Expert = dbExpert,
                                    Tag    = tag
                                };

                                dbExpert.ExpertTags.Add(newExpertTag);
                            }

                            unit.Complete();

                            if (dtoUser.MainFields == null)
                            {
                                return(BadRequest(new { message = "You must have some main fields" }));
                            }

                            foreach (var expertTag in dtoUser.MainFields)
                            {
                                var tag = unit.Tags.GetTagByName(expertTag) ?? new Tag {
                                    Name = expertTag
                                };

                                category.Tags.Add(tag);

                                var newMainField = new MainFieldTag()
                                {
                                    Expert = dbExpert,
                                    Tag    = tag
                                };

                                dbExpert.MainFields.Add(newMainField);
                            }

                            dbExpert.ExpertCategory = category;
                        }

                        unit.Complete();
                    }

                    if (userCreationResult.Succeeded)
                    {
                        return(Ok());
                    }

                    foreach (var error in userCreationResult.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                    return(BadRequest(ModelState));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }