Ejemplo n.º 1
0
        public async Task <IActionResult> GetPeriodList([FromBody] GetPeriodListRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await repository.AuthenticateUserToken(req.CurrentUser.UserId, req.CurrentUser.UserToken);

            if (user == null)
            {
                return(NotFound());
            }

            GetPeriodListResponse resp = new GetPeriodListResponse();

            resp.Periods = (await repository.GetPeriodsByCompanyId(user.CompanyId)).ConvertAll(x => LoadPeriod(x));

            resp.Periods = resp.Periods.OrderByDescending(x => x.StartDate).ToList();

            return(Ok(resp));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SavePeriodList([FromBody] SavePeriodListRequest req)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await repository.AuthenticateUserToken(req.CurrentUser.UserId, req.CurrentUser.UserToken);

            if (user == null)
            {
                return(NotFound());
            }

            if (req.Periods == null)
            {
                req.Periods = (await repository.GetPeriodsByCompanyId(user.CompanyId)).Select(x => LoadPeriod(x)).ToList();
            }

            try
            {
                if (req.IsBuild)
                {
                    req.Periods.AddRange(BuildPeriodList(req.FrequencyTypeId, req.StartDate, req.EndDate));
                }
            }catch (ArgumentException e)
            {
                return(NotFound(e));
            }

            GetPeriodListResponse resp = new GetPeriodListResponse();

            resp.Periods = new List <PeriodModel>(req.Periods.Count + 10);

            using (var transaction = await repository.StartTransaction())
            {
                try
                {
                    foreach (PeriodModel period in req.Periods)
                    {
                        try
                        {
                            if (!period.IsDeleted)
                            {
                                var result = await SavePeriodModel(user, period);

                                resp.Periods.Add(result);
                            }
                            else
                            {
                                Period existingPeriod = await repository.GetPeriodById(period.PeriodId);

                                if (existingPeriod == null)
                                {
                                    throw new NullReferenceException("Could not find the period with the id specified.");
                                }

                                repository.Delete(existingPeriod);
                                await repository.SaveChanges();
                            }
                        }
                        catch (NullReferenceException e)
                        {
                            throw new NullReferenceException($"Period: {period.PeriodId} error", e);
                        }
                        catch (UnauthorizedAccessException e)
                        {
                            throw new UnauthorizedAccessException($"Period: {period.PeriodId} is not editable by this user.", e);
                        }
                        catch (Exception e)
                        {
                            throw new Exception("Unknown Error Occured.", e);
                        }
                    }

                    transaction.Commit();
                }catch (Exception e)
                {
                    transaction.Rollback();

                    return(NotFound(e.InnerException));
                }

                resp.Periods = resp.Periods.OrderByDescending(x => x.StartDate).ToList();

                return(Ok(resp));
            }
        }