public async Task <IActionResult> Post([FromBody] CreateCategoryMaster category) { try { if (!ModelState.IsValid) { return(Ok(new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = true, StatusCode = ((int)ResponseStatus.BadRequest).ToString(), Message = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)) })); } CategoryDataAccess _categoryAccess = new CategoryDataAccess(_iconfiguration); DatabaseResponse response = await _categoryAccess.CreateCategory(category); if (response.ResponseCode == (int)DbReturnValue.CreateSuccess) { return(Ok(new OperationResponse { HasSucceeded = true, IsDomainValidationErrors = false, Message = EnumExtensions.GetDescription(DbReturnValue.CreateSuccess), ReturnedObject = response.Results })); } else { Log.Error(EnumExtensions.GetDescription(DbReturnValue.CreationFailed)); return(Ok(new OperationResponse { HasSucceeded = false, IsDomainValidationErrors = false, Message = EnumExtensions.GetDescription(DbReturnValue.RecordExists), ReturnedObject = response.Results })); } } catch (Exception ex) { Log.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); return(Ok(new OperationResponse { HasSucceeded = false, Message = StatusMessages.ServerError, StatusCode = ((int)ResponseStatus.ServerError).ToString(), IsDomainValidationErrors = false })); } }
public async Task <DatabaseResponse> CreateCategory(CreateCategoryMaster categoryMaster) { try { SqlParameter[] parameters = { new SqlParameter("@Name", SqlDbType.NVarChar), new SqlParameter("@CreatedBy", SqlDbType.Int), new SqlParameter("@Name_Ar", SqlDbType.NVarChar), new SqlParameter("@Weight", SqlDbType.Int) }; parameters[0].Value = categoryMaster.Name; parameters[1].Value = categoryMaster.CreatedBy; parameters[2].Value = categoryMaster.Name_Ar; parameters[3].Value = categoryMaster.Weight; _DataHelper = new DataAccessHelper("CreateCategory", parameters, _configuration); DataTable dt = new DataTable(); int result = await _DataHelper.RunAsync(dt); List <CategoryMaster> categories = new List <CategoryMaster>(); if (dt != null && dt.Rows.Count > 0) { categories = (from model in dt.AsEnumerable() select new CategoryMaster() { Id = model.Field <int>("Id"), Name = model.Field <string>("Name"), CreatedBy = model.Field <string>("CreatedBy"), CreatedOn = model.Field <DateTime>("CreatedOn"), UpdatedOn = model.Field <DateTime>("UpdatedOn"), UpdatedBy = model.Field <string>("UpdatedBy"), Active = model.Field <bool>("Active"), Name_Ar = model.Field <string>("Name_Ar") }).ToList(); } return(new DatabaseResponse { ResponseCode = result, Results = categories }); } catch (Exception ex) { Log.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical)); throw; } finally { _DataHelper.Dispose(); } }