Esempio n. 1
0
        public async Task <ISingleModelResponse <MatchDto> > GetMatchAsync(int id)
        {
            Logger?.LogInformation($"{nameof(GetMatchAsync)} has been invoked");

            var response = new SingleModelResponse <MatchDto>();

            try
            {
                var item = await MatchRepository.GetItems()
                           .Include(m => m.Team1)
                           .Include(m => m.Team2)
                           .Include(m => m.League)
                           .Include(m => m.Scores).ThenInclude(s => s.Player)
                           .FirstOrDefaultAsync(m => m.Id == id);

                if (ShouldIncludeAuditData())
                {
                    response.Model = Mapper.Map <MatchAuditDto>(item);
                }
                else
                {
                    response.Model = Mapper.Map <MatchDto>(item);
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
        public async Task <ISingleModelResponse <Order> > RemoveOrderAsync(Int32 id)
        {
            Logger?.LogInformation("{0} has been invoked", nameof(RemoveOrderAsync));

            var response = new SingleModelResponse <Order>();

            try
            {
                response.Model = await SalesRepository.GetOrderAsync(new Order(id));

                if (response.Model?.OrderDetails.Count > 0)
                {
                    throw new ForeignKeyDependencyException(String.Format(SalesDisplays.RemoveOrderExceptionMessage, id));
                }

                await SalesRepository.DeleteOrderAsync(response.Model);

                Logger?.LogInformation(SalesDisplays.DeleteOrderMessage);
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 3
0
        public async Task <ISingleModelResponse <TeamDto> > GetTeamAsync(int id)
        {
            Logger?.LogInformation($"{nameof(GetTeamAsync)} has been invoked");

            var response = new SingleModelResponse <TeamDto>();

            try
            {
                var item = await TeamRepository.GetItemByIdAsync(id);

                if (ShouldIncludeAuditData())
                {
                    response.Model = Mapper.Map <TeamAuditDto>(item);
                }
                else
                {
                    response.Model = Mapper.Map <TeamDto>(item);
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 4
0
        public async Task <ISingleModelResponse <PlayerDto> > GetPlayerAsync(int id)
        {
            Logger?.LogInformation($"{nameof(GetPlayerAsync)} has been invoked");

            var response = new SingleModelResponse <PlayerDto>();

            try
            {
                var item = await PlayerRepository.GetItems()
                           .Include(p => p.TeamAssignment)
                           .ThenInclude(pta => pta.Team)
                           .FirstOrDefaultAsync(p => p.Id == id);

                if (ShouldIncludeAuditData())
                {
                    response.Model = Mapper.Map <PlayerAuditDto>(item);
                }
                else
                {
                    response.Model = Mapper.Map <PlayerDto>(item);
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 5
0
        public async Task <ISingleModelResponse <MatchDto> > UpdateMatchResultsAsync(Match results)
        {
            Logger?.LogInformation($"{nameof(UpdateMatchResultsAsync)} has been invoked");

            var response = new SingleModelResponse <MatchDto>();

            try
            {
                var item = await MatchRepository.GetItems()
                           .Include(m => m.Scores)
                           .FirstOrDefaultAsync(m => m.Id == results.Id);

                if (item == null)
                {
                    throw new FlmException($"Match with id={results.Id} doesn't exist");
                }

                var homeTeamScores = results.Scores.Where(s => s.TeamId == item.Team1Id).Count();
                var awayTeamScores = results.Scores.Where(s => s.TeamId == item.Team2Id).Count();

                if (results.Team1Score != homeTeamScores || results.Team2Score != awayTeamScores)
                {
                    throw new FlmException($"Provided scores mismatch match results.");
                }

                // Update results

                item.Team1Score = results.Team1Score;
                item.Team2Score = results.Team2Score;

                // Remove existed score items that are not presented in updated results

                item.Scores.RemoveAll(oldScore => !results.Scores.Any(s => s.Id == oldScore.Id));

                // Add new scores

                var newScores = results.Scores.Where(s => s.Id == null);

                foreach (var score in newScores)
                {
                    item.Scores.Add(score);
                }

                await MatchRepository.UpdateItemAsync(item);

                // TODO: As we really do not need a result of this task call it in background. Consider using Hangfire.
                await LeagueService.RecalculateLeagueStandingsAsync(item.LeagueId.Value);

                // Return updated item with scores

                return(await GetMatchAsync(item.Id.Value));
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 6
0
        public async Task <ISingleModelResponse <Order> > GetOrderAsync(Int32 id)
        {
            Logger?.LogInformation("{0} has been invoked", nameof(GetOrderAsync));

            var response = new SingleModelResponse <Order>();

            try
            {
                response.Model = await SalesRepository.GetOrderAsync(new Order(id));
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 7
0
        public async Task <ISingleModelResponse <Order> > CloneOrderAsync(Int32 id)
        {
            Logger?.LogInformation("{0} has been invoked", nameof(CloneOrderAsync));

            var response = new SingleModelResponse <Order>();

            try
            {
                var entity = await SalesRepository.GetOrderAsync(new Order(id));

                if (entity != null)
                {
                    response.Model = new Order();

                    response.Model.OrderID    = entity.OrderID;
                    response.Model.OrderDate  = entity.OrderDate;
                    response.Model.CustomerID = entity.CustomerID;
                    response.Model.EmployeeID = entity.EmployeeID;
                    response.Model.ShipperID  = entity.ShipperID;
                    response.Model.Total      = entity.Total;
                    response.Model.Comments   = entity.Comments;

                    if (entity.OrderDetails != null && entity.OrderDetails.Count > 0)
                    {
                        foreach (var detail in entity.OrderDetails)
                        {
                            response.Model.OrderDetails.Add(new OrderDetail
                            {
                                ProductID   = detail.ProductID,
                                ProductName = detail.ProductName,
                                UnitPrice   = detail.UnitPrice,
                                Quantity    = detail.Quantity,
                                Total       = detail.Total
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
        public async Task <ISingleModelResponse <Employee> > UpdateEmployeeAsync(Employee changes)
        {
            Logger?.LogInformation("{0} has been invoked", nameof(UpdateEmployeeAsync));

            var response = new SingleModelResponse <Employee>() as ISingleModelResponse <Employee>;

            try
            {
                await HumanResourcesRepository.UpdateEmployeeAsync(changes);

                response.Model = changes;
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 9
0
        public async Task <ISingleModelResponse <Farm> > GetFarmAsync(int vendorId)
        {
            {
                Logger?.LogInformation("{0} has been invoked", nameof(GetFarmAsync));

                var response = new SingleModelResponse <Farm>();

                try
                {
                    response.Model = await FarmRepository.GetFarmAsync(vendorId);
                }
                catch (Exception ex)
                {
                    response.SetError(ex, Logger);
                }

                return(response);
            }
        }
Esempio n. 10
0
        public async Task <ISingleModelResponse <Team> > CreateTeamAsync(Team item)
        {
            Logger?.LogInformation($"{nameof(CreateTeamAsync)} has been invoked");

            var response = new SingleModelResponse <Team>();

            try
            {
                await TeamRepository.AddItemAsync(item);

                response.Model = item;
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 11
0
        public async Task <ISingleModelResponse <League> > UpdateLeagueAsync(League item)
        {
            Logger?.LogInformation($"{nameof(UpdateLeagueAsync)} has been invoked");

            var response = new SingleModelResponse <League>();

            try
            {
                await LeagueRepository.UpdateItemAsync(item);

                response.Model = item;
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 12
0
        public async Task <ISingleModelResponse <Team> > RemoveTeamAsync(int id)
        {
            Logger?.LogInformation($"{nameof(RemoveTeamAsync)} has been invoked");

            var response = new SingleModelResponse <Team>();

            try
            {
                response.Model = await TeamRepository.GetItemByIdAsync(id);

                if (response.Model != null)
                {
                    await TeamRepository.RemoveItemAsync(response.Model);
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 13
0
        public async Task <ISingleModelResponse <Order> > RemoveOrderAsync(Int32 id)
        {
            Logger?.LogInformation("{0} has been invoked", nameof(RemoveOrderAsync));

            var response = new SingleModelResponse <Order>();

            try
            {
                response.Model = await SalesRepository.GetOrderAsync(new Order(id));

                if (response.Model?.OrderDetails.Count > 0)
                {
                    throw new ForeignKeyDependencyException(
                              String.Format("Order with ID: {0} cannot be deleted, because has dependencies. Please contact to technical support for more details", id)
                              );
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }
Esempio n. 14
0
        public async Task <ISingleModelResponse <Order> > CreateOrderAsync(Order header, OrderDetail[] details)
        {
            Logger?.LogInformation("{0} has been invoked", nameof(CreateOrderAsync));

            var response = new SingleModelResponse <Order>();

            try
            {
                using (var transaction = await DbContext.Database.BeginTransactionAsync())
                {
                    var warehouses = await ProductionRepository.GetWarehouses().ToListAsync();

                    try
                    {
                        foreach (var detail in details)
                        {
                            var product = await ProductionRepository.GetProductAsync(new Product { ProductID = detail.ProductID });

                            if (product == null)
                            {
                                throw new NonExistingProductException(
                                          String.Format("Sent order has a non existing product with ID: '{0}', order has been cancelled.", detail.ProductID)
                                          );
                            }
                            else
                            {
                                detail.ProductName = product.ProductName;
                            }

                            if (product.Discontinued == true)
                            {
                                throw new AddOrderWithDiscontinuedProductException(
                                          String.Format("Product with ID: '{0}' is discontinued, order has been cancelled.", product.ProductID)
                                          );
                            }

                            detail.UnitPrice = product.UnitPrice;
                            detail.Total     = product.UnitPrice * detail.Quantity;
                        }

                        header.Total = details.Sum(item => item.Total);

                        await SalesRepository.AddOrderAsync(header);

                        foreach (var detail in details)
                        {
                            detail.OrderID = header.OrderID;

                            await SalesRepository.AddOrderDetailAsync(detail);

                            var lastInventory = ProductionRepository
                                                .GetProductInventories()
                                                .Where(item => item.ProductID == detail.ProductID)
                                                .OrderByDescending(item => item.CreationDateTime)
                                                .FirstOrDefault();

                            var stocks = lastInventory == null ? 0 : lastInventory.Stocks - detail.Quantity;

                            var productInventory = new ProductInventory
                            {
                                ProductID        = detail.ProductID,
                                WarehouseID      = warehouses.First().WarehouseID,
                                CreationDateTime = DateTime.Now,
                                Quantity         = detail.Quantity * -1,
                                Stocks           = stocks
                            };

                            await ProductionRepository.AddProductInventoryAsync(productInventory);
                        }

                        response.Model = header;

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

                        throw ex;
                    }
                }
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }