Example #1
0
        public async Task <IActionResult> Create(PlanServiceModel model)
        {
            model.UserId = GetCurrentUser().Id;
            PlanResponseModel result = await _planService.CreatePlanAsync(model);

            return(Json(result));
        }
Example #2
0
        private async Task Subscribe(Message message, string planId)
        {
            try
            {
                var plan = await _planService.GetByIdAsync(planId);

                if (plan == null)
                {
                    await _botService.Client.SendTextMessageAsync(message.Chat.Id, "Plan not found");

                    return;
                }

                PlanServiceModel result = await _botSubscriptionService.CreateBotSubscriptionAsync(
                    new BotSubscriptionServiceModel
                {
                    ChatId = message.Chat.Id.ToString(),
                    PlanId = planId
                });

                await _botService.Client.SendTextMessageAsync(message.Chat.Id,
                                                              $"You have successfully subscribed to '{result.Name}' plan.");
            }
            catch (DomainServicesException exception)
            {
                await _botService.Client.SendTextMessageAsync(message.Chat, exception.Message);
            }
        }
Example #3
0
        public async Task Update([FromBody] PlanServiceModel model)
        {
            var plan = await _planService.GetByIdAsync(model.Id);

            ValidateUser(plan.UserId);

            await _planService.UpdateAsync(model);
        }
        public async Task <PlanResponseModel> CreatePlanAsync(PlanServiceModel model)
        {
            var plan = await CreatePlanCoreAsync(model);

            return(new PlanResponseModel
            {
                Name = plan.Name,
                Id = plan.Id
            });
        }
        public async Task UpdateAsync(PlanServiceModel model)
        {
            Plan plan = await _planObjectService.GetByIdAsync <Plan>(model.Id);

            if (plan == null)
            {
                throw new DomainServicesException("Plan not found.");
            }

            plan.Name = model.Name;

            await _planObjectService.UpdateAsync(plan);
        }
        private async Task <Plan> CreatePlanCoreAsync(PlanServiceModel model, string newPlanId = null)
        {
            var plan = new Plan(model.Name, model.UserId)
            {
                IsTemplate = model.IsTemplate
            };

            if (!string.IsNullOrEmpty(newPlanId))
            {
                plan.Id = newPlanId;
            }

            await _planObjectService.CreateAsync(plan);

            foreach (PlanAreaServiceModel planArea in model.PlanAreas)
            {
                PlanArea area = new PlanArea
                {
                    Name   = planArea.Name,
                    Plan   = plan,
                    PlanId = plan.Id,
                    UserId = plan.UserId
                };

                await _planAreaObjectService.CreateAsync(area);

                foreach (AreaTopicServiceModel areaTopic in planArea.AreaTopics)
                {
                    await _topicObjectService.CreateAsync(new AreaTopic
                    {
                        Name      = areaTopic.Name,
                        StartDate = areaTopic.StartDate != null ? DateTime.ParseExact(areaTopic.StartDate, "yyyy-MM-dd",
                                                                                      CultureInfo.CurrentCulture) : (DateTime?)null,
                        EndDate = areaTopic.EndDate != null
                            ? DateTime.ParseExact(areaTopic.EndDate, "yyyy-MM-dd", CultureInfo.CurrentCulture)
                            : (DateTime?)null,
                        Source      = areaTopic.Source,
                        Description = areaTopic.Description,
                        PlanArea    = area,
                        PlanAreaId  = area.Id,
                        UserId      = areaTopic.UserId,
                        PlanId      = areaTopic.PlanId
                    });
                }
            }

            return(plan);
        }
Example #7
0
        public async Task UpdateAsync(PlanServiceModel model)
        {
            using (_unitOfWork)
            {
                Plan plan = await _planReadRepository.GetByIdAsync(model.Id);

                if (plan == null)
                {
                    throw new DomainServicesException("Plan not found.");
                }

                plan.Name = model.Name;

                await _unitOfWork.CommitAsync();
            }
        }
Example #8
0
        public async Task <PlanResponseModel> CreatePlanAsync(PlanServiceModel model)
        {
            using (_unitOfWork)
            {
                var plan = new Plan(model.Name, model.UserId);

                await _planWriteRepository.CreateAsync(plan);

                foreach (PlanAreaServiceModel planArea in model.PlanAreas)
                {
                    PlanArea area = new PlanArea
                    {
                        Name   = planArea.Name,
                        Plan   = plan,
                        PlanId = plan.Id
                    };

                    await _planAreaRepository.CreateAsync(area);

                    foreach (AreaTopicServiceModel areaTopic in planArea.AreaTopics)
                    {
                        await _areaTopicRepository.CreateAsync(new AreaTopic
                        {
                            Name      = areaTopic.Name,
                            StartDate = DateTime.ParseExact(areaTopic.StartDate, "yyyy-MM-dd",
                                                            CultureInfo.CurrentCulture),
                            EndDate     = DateTime.ParseExact(areaTopic.EndDate, "yyyy-MM-dd", CultureInfo.CurrentCulture),
                            Source      = areaTopic.Source,
                            Description = areaTopic.Description,
                            PlanArea    = area,
                            PlanAreaId  = area.Id
                        });
                    }
                }

                await _unitOfWork.CommitAsync();

                return(new PlanResponseModel
                {
                    Name = plan.Name,
                    Id = plan.Id
                });
            }
        }
Example #9
0
        private static async Task Subscribe(Message message, string planId)
        {
            IServiceScope           scope = _serviceProvider.CreateScope();
            IBotSubscriptionService botSubscriptionService =
                (IBotSubscriptionService)scope.ServiceProvider.GetService(typeof(IBotSubscriptionService));

            try
            {
                PlanServiceModel result = await botSubscriptionService.CreateBotSubscriptionAsync(
                    new BotSubscriptionServiceModel
                {
                    ChatId = message.Chat.Id.ToString(),
                    PlanId = planId
                });

                await _client.SendTextMessageAsync(message.Chat.Id,
                                                   $"You have successfully subscribed to '{result.Name}' plan.");
            }
            catch (DomainServicesException exception)
            {
                await _client.SendTextMessageAsync(message.Chat.Id, exception.Message);
            }
        }
Example #10
0
        public async Task <PlanResponseModel> ImportDataFromSheetAsync(ImportGoogleSheetModel model)
        {
            string userId = ((User)_httpContextAccessor.HttpContext.Items["User"])?.Id;

            if (string.IsNullOrEmpty(userId))
            {
                throw new UnauthorizedAccessException("User not found");
            }
            PlanServiceModel planServiceModel = new PlanServiceModel
            {
                IsTemplate = model.IsTemplate,
                UserId     = userId,
                Name       = model.PlanName,
            };

            var sheet = await _sheetsService.Spreadsheets.Values.Get(model.SheetId, model.Range).ExecuteAsync();

            var    areas       = new Dictionary <string, List <AreaTopicServiceModel> >();
            string previousKey = string.Empty;

            foreach (var sheetValue in sheet.Values.ToList())
            {
                int sheetValueCount = sheetValue.Count - 1;
                if (model.AreaColumn > sheetValueCount ||
                    model.TopicColumn > sheetValueCount)
                {
                    throw new DomainServicesException("Invalid column.");
                }

                string area = sheetValue[model.AreaColumn].ToString();
                if (!string.IsNullOrEmpty(area))
                {
                    previousKey = area;
                }
                string key = string.IsNullOrWhiteSpace(area) ? previousKey : area;
                if (areas.ContainsKey(key))
                {
                    areas[key].Add(new AreaTopicServiceModel
                    {
                        Name        = sheetValue[model.TopicColumn].ToString(),
                        Description = model.DescriptionColumn.HasValue && model.DescriptionColumn < sheetValueCount ? sheetValue[model.DescriptionColumn.Value].ToString() : null,
                        StartDate   = model.StartDateColumn.HasValue && model.StartDateColumn < sheetValueCount ? sheetValue[model.StartDateColumn.Value].ToString() : null,
                        EndDate     = model.EndDateColumn.HasValue && model.EndDateColumn < sheetValueCount ? sheetValue[model.EndDateColumn.Value].ToString() : null,
                        Source      = model.SourceColumn.HasValue && model.SourceColumn < sheetValueCount ? sheetValue[model.SourceColumn.Value].ToString() : null,
                        UserId      = userId
                    });
                }
                else
                {
                    areas.Add(key, new List <AreaTopicServiceModel>
                    {
                        new AreaTopicServiceModel
                        {
                            Name        = sheetValue[model.TopicColumn].ToString(),
                            Description = model.DescriptionColumn.HasValue && model.DescriptionColumn < sheetValueCount ? sheetValue[model.DescriptionColumn.Value].ToString() : null,
                            StartDate   = model.StartDateColumn.HasValue && model.StartDateColumn < sheetValueCount ? sheetValue[model.StartDateColumn.Value].ToString() : null,
                            EndDate     = model.EndDateColumn.HasValue && model.EndDateColumn < sheetValueCount ? sheetValue[model.EndDateColumn.Value].ToString() : null,
                            Source      = model.SourceColumn.HasValue && model.SourceColumn < sheetValueCount ? sheetValue[model.SourceColumn.Value].ToString() : null,
                            UserId      = userId
                        }
                    });
                }
            }

            planServiceModel.PlanAreas = areas.Select(x => new PlanAreaServiceModel
            {
                Name       = x.Key,
                AreaTopics = x.Value.ToArray()
            }).ToArray();

            return(await _planService.CreatePlanAsync(planServiceModel));
        }
 public void UpdatePlan(PlanServiceModel data, Action <string> success, Action <string> failure)
 {
     data.recurring = PlanOperationType.EditPlan.ToDescription();
     ExecuteAction(data, success, failure);
 }