Example #1
0
        public async Task <IActionResult> CreateOrUpdate([FromBody] MaintenanceNewModel model)
        {
            ProcessResult  result         = null;
            HttpStatusCode httpStatusCode = HttpStatusCode.InternalServerError;

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

                result = await _maintenanceService.CreateOrUpdateAsync(model);
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex, this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod().Name);
                httpStatusCode = HttpStatusCode.InternalServerError;
                return(StatusCode((int)httpStatusCode));
            }
            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors));
            }

            return(Ok());
        }
        public async Task <ProcessResult> CreateOrUpdateAsync(MaintenanceNewModel model)
        {
            bool        isCreated = false;
            Func <Task> action    = async() =>
            {
                try
                {
                    var maintenance = await dbOperator.Maintenances.Where(x => x.ID == model.ID).SingleOrDefaultAsync();

                    if (maintenance == null)
                    {
                        isCreated   = true;
                        maintenance = new Maintenance();
                    }
                    else
                    {
                        if (CurrentUser.Id != maintenance.Status.CreatedBy)
                        {
                            throw new InvalidOperationException("Yalnızca arızanın sahibi güncelleme yapabilmektedir.");
                        }
                    }

                    maintenance.Status      = PrepareStatusBase(maintenance.Status == null ? new StatusBase() : maintenance.Status, isCreated);
                    maintenance.Description = model.Description;
                    await dbOperator.AddAsync(maintenance);

                    await dbOperator.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    Utilities.LogError(ex, this.GetType().Name + ":" + System.Reflection.MethodBase.GetCurrentMethod().Name);
                }
            };

            return(await Process.RunAsync(action));
        }