Example #1
0
        public async Task <ActionResponse> AddAsync(int userId, ReportSubscriptionModel model)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;
                try
                {
                    var user = unitWork.UserRepository.GetByID(userId);
                    if (user == null)
                    {
                        mHelper          = new MessageHelper();
                        response.Message = mHelper.GetNotFound("User");
                        response.Success = false;
                        return(response);
                    }

                    if (model.ReportIds.Count > 0)
                    {
                        var strategy = context.Database.CreateExecutionStrategy();
                        await strategy.ExecuteAsync(async() =>
                        {
                            using (var transaction = context.Database.BeginTransaction())
                            {
                                var reports             = await unitWork.ReportsRepository.GetManyQueryableAsync(r => model.ReportIds.Contains(r.Id));
                                var deleteSubscriptions = await unitWork.ReportSubscriptionRepository.GetManyQueryableAsync(s => s.UserId == userId);

                                foreach (var sub in deleteSubscriptions)
                                {
                                    unitWork.ReportSubscriptionRepository.Delete(sub);
                                }
                                await unitWork.SaveAsync();

                                foreach (var report in reports)
                                {
                                    unitWork.ReportSubscriptionRepository.Insert(new EFReportSubscriptions()
                                    {
                                        Report = report,
                                        User   = user
                                    });
                                }
                                await unitWork.SaveAsync();
                                transaction.Commit();
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
                return(response);
            }
        }
Example #2
0
        public async Task <IActionResult> Subscribe([FromBody] ReportSubscriptionModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userIdVal = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            if (string.IsNullOrEmpty(userIdVal))
            {
                return(BadRequest("Invalid attempt"));
            }
            int userId   = Convert.ToInt32(userIdVal);
            var response = await reportSubscriptionService.AddAsync(userId, model);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }
            return(Ok(true));
        }