Exemplo n.º 1
0
        public async Task <IActionResult> PostTraining([FromBody] Training training)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(PostTraining));

            var response = new SingleResponse <Training>();

            try
            {
                //Save to database  if Training End date is greater than start date
                if (training.Training_Endate > training.Training_Startdate)
                {
                    _context.Training.Add(training);
                    await _context.SaveChangesAsync();

                    response.NumofDays = (int?)(training.Training_Endate - training.Training_Startdate)?.TotalDays; // Return the number of days
                    response.Model     = training;
                }
                else
                {
                    response.IsError      = true;
                    response.ErrorMessage = Constants.EndDateBeforeStartDateError;
                }
            }
            catch (Exception ex)
            {
                response.IsError      = true;
                response.ErrorMessage = "There was an internal error, please contact to technical support.";

                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostTraining), ex);
            }

            return(response.ToHttpCreatedResponse());
        }
        public async Task <IActionResult> PostStockItemAsync([FromBody] PostStockItemsRequest request)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(PostStockItemAsync));

            var response = new SingleResponse <StockItem>();

            try
            {
                var existingEntity = await DbContext
                                     .GetStockItemsByStockItemNameAsync(new StockItem { StockItemName = request.StockItemName });

                if (existingEntity != null)
                {
                    ModelState.AddModelError("StockItemName", "Stock item name already exists");
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                // Create entity from request model
                var entity = request.ToEntity();

                // Add entity to repository
                DbContext.Add(entity);

                // Save entity in database
                await DbContext.SaveChangesAsync();

                // Set the entity to response model
                response.Model = entity;
            }
            catch (Exception ex)
            {
                response.DidError     = true;
                response.ErrorMessage = "There was an internal error, please contact to technical support.";

                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostStockItemAsync), ex);
            }

            return(response.ToHttpCreatedResponse());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PostJobAsync([FromBody] PostJobRequest request)
        {
            var response = new SingleResponse <Job>();

            try
            {
                var existingEntity = await DbContext
                                     .GetJobsByJobTitleAsync(new Job { JobTitle = request.JobTitle });

                if (existingEntity != null)
                {
                    ModelState.AddModelError("JobTitle", "Job already exists");
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                // Create entity from request model
                var entity = request.ToEntity();

                // Add entity to repository
                DbContext.Add(entity);

                // Save entity in database
                await DbContext.SaveChangesAsync();

                // Set the entity to response model
                response.Model = entity;
            }
            catch (Exception ex)
            {
                response.DidError     = true;
                response.ErrorMessage = "There was an internal error";

                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostJobAsync), ex);
            }

            return(response.ToHttpCreatedResponse());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddProductAsync([FromBody] AddProductRequest request)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(AddProductAsync));

            // Validate request model
            if (!ModelState.IsValid)
            {
                return(BadRequest(request));
            }

            var response = new SingleResponse <AddProductRequest>();

            try
            {
                var entity = request.ToEntity();

                // Check if entity exists
                var existingProduct = await Service.DbContext.GetProductByProductNameAsync(entity);

                if (existingProduct != null)
                {
                    ModelState.AddModelError("ProductName", "Product name already exists");

                    return(BadRequest(ModelState));
                }

                entity.CreationUser = User.GetUserName();

                await Service.CreateProductAsync(entity);

                response.Model = entity.ToAddProductRequest();

                Logger?.LogInformation("The entity was created successfully.");
            }
            catch (Exception ex)
            {
                response.SetError(Logger, nameof(AddProductAsync), ex);
            }

            return(response.ToHttpCreatedResponse());
        }