Exemplo n.º 1
0
        public async Task <ResponseModel> FindBy(UserSearchEnum by, string value)
        {
            var enpoint = string.Empty;

            switch (by)
            {
            case UserSearchEnum.Email:
                enpoint = "api/user/search/email/" + value;
                break;

            case UserSearchEnum.Username:
                enpoint = "api/user/search/username/" + value;
                break;
            }

            return(await SendRequest <Models.User>(RequestVerbs.GET, enpoint, "user"));
        }
Exemplo n.º 2
0
        public IHttpActionResult GetSurveys([FromUri] SurveyQueryParams obj, int page, int size,
                                            int?groupId, UserSearchEnum userEnum, int categoryId, bool subCategories, bool?isPublic,
                                            bool allSurveys, int state)
        {
            groupId = groupId == 0 ? null : groupId;

            int?authorId       = null;
            int?filledUserId   = null;
            int?favoriteUserId = null;
            int?groupUserId    = null;

            if (!(bool)isPublic)
            {
                isPublic = null;
            }
            var identity = (ClaimsIdentity)User.Identity;

            if (!identity.IsAuthenticated)
            {
                if (groupId != null)
                {
                    return(Unauthorized());
                }
                isPublic = true;
            }
            else
            {
                var userId = int.Parse(identity.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value);
                var role   = identity.Claims.Where(c => c.Type == ClaimTypes.Role).FirstOrDefault().Value;

                if (groupId != null)
                {
                    var userGroups = surveyService.GetUserGroups(userId);
                    if (!userGroups.Contains((int)groupId) && userId != surveyService.GetGroupAuthor((int)groupId))
                    {
                        return(Unauthorized());
                    }
                }
                //all surveys for user - public and all groups for user
                if (role.Equals("User") && allSurveys)
                {
                    isPublic    = true;
                    groupUserId = userId;
                }
                if (!role.Equals("User") && allSurveys)
                {
                    isPublic = null;
                }
                if (allSurveys)
                {
                    switch (userEnum)
                    {
                    case UserSearchEnum.AUTHOR:
                        authorId = userId;
                        break;

                    case UserSearchEnum.FILLSURVEY:
                        filledUserId = userId;
                        break;

                    case UserSearchEnum.FAVORITESURVEY:
                        favoriteUserId = userId;
                        break;

                    case UserSearchEnum.GROUP:
                        groupUserId = userId;
                        break;

                    default:
                        break;
                    }
                }
            }
            if (categoryId >= 0)
            {
                if (categoryId == 0 && subCategories == false)
                {
                }
                else
                {
                    if (subCategories == true && categoryId > 0)
                    {
                        obj.ListOfCategories = surveyService.GetAllSubCategories((int)categoryId);
                    }
                    if (obj.ListOfCategories == null)
                    {
                        obj.ListOfCategories = new List <int>();
                    }
                    obj.ListOfCategories.Add((int)categoryId);
                }
            }


            Tuple <IEnumerable <Survey>, int> returnValue = null;

            try
            {
                if (obj == null)
                {
                    return(BadRequest());
                }
                else
                {
                    obj.ListOfCategories = obj.ListOfCategories == null ? new List <int>() : obj.ListOfCategories;
                    returnValue          = surveyService.GetActiveSurveys(page, size, isPublic, filledUserId, authorId, favoriteUserId, groupUserId, groupId,
                                                                          obj.Name, obj.Description, obj.AuthorFirstName, obj.AuthorLastName, obj.QuestionText, obj.ListOfCategories, state);
                }


                List <SurveyViewModel> retVal = AutoMapper.Mapper.Map <List <Survey>, List <SurveyViewModel> >(returnValue.Item1.ToList());

                this.SetSurveyVMValues(retVal, returnValue.Item1.ToList());

                PageModel <SurveyViewModel> pageModel = new PageModel <SurveyViewModel>()
                {
                    Models      = retVal,
                    Size        = size,
                    CurrentPage = page,
                    Count       = returnValue.Item2
                };

                return(Ok(pageModel));
            }
            catch (Exception e)
            {
                return(BadRequest());
            }
        }