public async Task <ActionResult> PutCommunity(Community community)
        {
            CommunityService service = CommunityService.GetInstance();


            CreateCommunity newCommunity = new CreateCommunity()
            {
                Id             = community.Id,
                Name           = community.Name,
                Description    = community.Description,
                Local          = community.Local,
                FoundationDate = community.date,
                Site           = community.Site,
                GitHub         = community.GitHub,
                Mail           = community.Mail,
                Avatar         = community.Avatar == null ?null: community.Avatar.InputStream,
                UserId         = User.Identity.GetUserId()
            };


            var updated = await service.UpdateCommunity(newCommunity);

            if (updated.Success)
            {
                return(Redirect(Request.UrlReferrer.PathAndQuery));
            }
            return(PartialView("_UpdateCommunity", community));
        }
        public Task <int> PostAsync(CreateCommunity item)
        {
            return(Task.Factory.StartNew(() =>
            {
                SqlParameter sponsors;
                SqlParameter tags;
                format_tags_and_sponsors_to_parameter(item.Tags, item.Sponsors, out sponsors, out tags);

                var name = SqlParameterFactory.getVarCharParameter("name", item.Name);
                var local = SqlParameterFactory.getVarCharParameter("local", item.Local);
                var latitude = SqlParameterFactory.getVarCharParameter("latitude", item.Latitude);
                var longitude = SqlParameterFactory.getVarCharParameter("longitude", item.Longitude);
                var description = SqlParameterFactory.getVarCharParameter("description", item.Description);
                var date = SqlParameterFactory.getDateParameter("date", item.FoundationDate);
                var avatar = SqlParameterFactory.getVarCharParameter("avatar", item.AvatarLink);    //para não alterar o procedimento (muito trabalho...)
                var userId = SqlParameterFactory.getVarCharParameter("userId", item.UserId);

                ////Parameter for SP output
                var result = new SqlParameter("result", SqlDbType.Int);
                result.Direction = ParameterDirection.Output;

                try
                {
                    context.Database.ExecuteSqlCommand("EXEC insert_community @name,@local,@description,@date,@avatar,@userId,@latitude,@longitude,@sponsors,@tags,@result OUTPUT", name, local, description, date, avatar, userId, latitude, longitude, sponsors, tags, result);

                    return Convert.ToInt32(result.Value);
                }
                catch (SqlException ex)
                {
                    throw new ArgumentException(ex.Message);
                }
            }));
        }
예제 #3
0
        public async Task <IActionResult> CreateCommunity(CreateCommunity input)
        {
            var token  = GetToken();
            var userId = LoginHelper.GetClaim(token, "UserId");

            input.UserId = Guid.Parse(userId);
            var result = await _communityAppService.CreateCommunity(input);

            return(Ok(result));
        }
예제 #4
0
        //<------------------------------------------------------------------------------------------------------>
        //<------------------------------------------------------------------------------------------------------>


        private async Task <OperationResult <int> > Put(CreateCommunity item)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var community = await communityRepo.GetByIdAsync(item.Id);

                if (community == null)
                {
                    return new OperationResult <int>()
                           {
                               Success = false, Message = Messages.COMMUNITY_NOT_EXIST
                           }
                }
                ;
                if (community.admins.FirstOrDefault(user => user.id == item.UserId) == null)
                {
                    return new OperationResult <int>()
                           {
                               Success = false, Message = Messages.USER_NOT_PERMISSION
                           }
                }
                ;

                string img = null;
                if (item.Avatar != null)
                {
                    img = (await ImageService.SendToProvider(item.Name, ImageService.ImageIdentity.Communities, item.Avatar, ImageService.ImageType.Avatar)).Result;
                }
                try
                {
                    community.avatar         = img == null ? community.avatar : img;
                    community.description    = item.Description == null ? community.description : item.Description;
                    community.foundationDate = item.FoundationDate == DateTime.MinValue ? community.foundationDate : item.FoundationDate;
                    community.local          = item.Local == null ? community.local : item.Local;
                    community.site           = item.Site == null ? community.site : item.Site;
                    community.mail           = item.Mail == null ? community.mail : item.Mail;
                    community.gitHub         = item.GitHub == null ? community.gitHub : item.GitHub;

                    var id = await communityRepo.PutAsync(community);

                    scope.Complete();
                    return(new OperationResult <int>()
                    {
                        Success = true, Message = Messages.COMMUNITY_UPDATED_SUCCESS, Result = id
                    });
                }
                catch (Exception ex)
                {
                    return(new OperationResult <int>()
                    {
                        Success = false, Message = ex.InnerException.Message
                    });
                }
            }
        }
예제 #5
0
        private async Task <OperationResult <int> > Post(CreateCommunity item)
        {
            if (item.Name == null)
            {
                return new OperationResult <int>()
                       {
                           Success = false, Message = Messages.COMMUNITY_NAME
                       }
            }
            ;
            var user = await userService.GetByIdAsync(item.UserId);

            if (!user.Success)
            {
                return new OperationResult <int>()
                       {
                           Success = false, Message = user.Message
                       }
            }
            ;


            try
            {
                if (item.Tags != null)
                {
                    item.Tags = item.Tags.Distinct().ToArray();
                }
                if (item.Sponsors != null)
                {
                    item.Sponsors = item.Sponsors.Distinct().ToArray();
                }


                string img = (await ImageService.SendToProvider(item.Name, ImageService.ImageIdentity.Communities, item.Avatar, ImageService.ImageType.Avatar)).Result;

                item.AvatarLink = img;
                var id = await communityRepo.PostAsync(item);

                return(new OperationResult <int>()
                {
                    Success = true, Message = Messages.COMMUNITY_CREATED_SUCCESS, Result = id
                });
            }
            catch (Exception ex)
            {
                ImageService.DeleteFolder(item.Name, ImageService.ImageIdentity.Communities);
                return(new OperationResult <int>()
                {
                    Success = false, Message = ex.Message
                });
            }
        }
예제 #6
0
        private async Task <OperationResult <int> > Delete(CreateCommunity item)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var community = await GetByIdAsync(item.Id);

                if (!community.Success)
                {
                    return new OperationResult <int>()
                           {
                               Success = false, Message = community.Message
                           }
                }
                ;
                if (!community.Result.admins.Select(elem => elem.id).Contains(item.UserId))
                {
                    return new OperationResult <int>()
                           {
                               Success = false, Message = "the user with id(" + item.UserId + ") does not have permission to delete this community"
                           }
                }
                ;

                try
                {
                    var id = await communityRepo.DeleteAsync(community.Result);

                    scope.Complete();
                    ImageService.DeleteFolder(item.Name, ImageService.ImageIdentity.Communities);
                    return(new OperationResult <int>()
                    {
                        Success = true, Message = Messages.COMMUNITY_DELETED_SUCCESS, Result = id
                    });
                }
                catch (Exception ex)
                {
                    return(new OperationResult <int>()
                    {
                        Success = false, Message = ex.InnerException.Message
                    });
                }
            }
        }
        public async Task <ActionResult> Create(Community community)
        {
            if (ModelState.IsValid)
            {
                CommunityService service = CommunityService.GetInstance();
                //ImageCommunityService imgService = new ImageCommunityService(service);

                CreateCommunity newCommunity = new CreateCommunity()
                {
                    Name           = community.Name,
                    Description    = community.Description,
                    Local          = community.Local,
                    FoundationDate = community.date,
                    Avatar         = community.Avatar == null?null:community.Avatar.InputStream,
                    UserId         = User.Identity.GetUserId(),
                    Latitude       = community.Latitude,
                    Longitude      = community.Longitude
                };
                var res = await service.CreateCommunity(newCommunity);

                if (res.Success)
                {
                    return(RedirectToAction("Get", new { id = community.Name }));
                }
                else
                {
                    ViewBag.Error   = true;
                    ViewBag.Message = res.Message;
                }
                return(View(community));
            }
            else
            {
                return(View(community));
            }
        }
예제 #8
0
 public async Task <OperationResult <int> > UpdateCommunity(CreateCommunity item)
 {
     return(await Put(item));
 }
예제 #9
0
 public async Task <OperationResult <int> > DeleteCommunity(CreateCommunity item)
 {
     return(await Delete(item));
 }
예제 #10
0
 public async Task <OperationResult <int> > CreateCommunity(CreateCommunity item)
 {
     return(await Post(item));//extraido para outro método, para caso, este método queira interagir com outras componentes, o código ficar legível
 }
        public async Task <Response> CreateCommunity(CreateCommunity input)
        {
            var responseModel = new Response();

            var userComs = await _communityUserRepository.GetAll()
                           .Where(x => x.UserId == input.UserId && x.IsAdmin && x.IsDeleted == false)
                           .ToListAsync();

            var user = await _userRepository.GetByIdAsync(input.UserId);

            if (userComs.Count > 1 && !user.IsAdmin)
            {
                responseModel.Status  = false;
                responseModel.Message = "Daha fazla topluluk oluşturamazsınız";
                return(responseModel);
            }

            var slug = Slug.FriendlyUrlTitle(input.Name);

            var isExist = _communityRepository.GetAll().Any(x => x.Slug == slug);

            if (isExist)
            {
                responseModel.Status  = false;
                responseModel.Message = "Bu isimde bir topluluk zaten var";
                return(responseModel);
            }

            var category = await _categoryRepository.GetAll().FirstOrDefaultAsync(x => x.Slug == input.CatSlug);

            var model = new Community
            {
                Name        = input.Name,
                Description = input.Description,
                CategoryId  = category.Id,
                Slug        = slug
            };

            if (input.LogoFile != null)
            {
                var path = await _blobService.InsertFile(input.LogoFile);

                model.LogoPath = path;
            }
            if (input.CoverImage != null)
            {
                var path = await _blobService.InsertFile(input.CoverImage);

                model.CoverImagePath = path;
            }
            var result = await _communityRepository.AddAsync(model);

            var communityUser = new CommunityUser
            {
                CommunityId = result.Id,
                UserId      = input.UserId,
                IsAdmin     = true
            };
            await _communityUserRepository.AddAsync(communityUser);

            responseModel.Message = "Topluluk başarıyla oluşturuldu";
            responseModel.Status  = true;
            responseModel.Slug    = model.Slug;
            return(responseModel);
        }