コード例 #1
0
        public ProductDto CreateOrUpdateProduct(ProductDto dto, int RequestUserId, string TokenKey)
        {
            try
            {
                CheckAuthentication(RequestUserId, TokenKey);

                #region Empty Control
                if (dto.Name.IsNullOrEmpty())
                {
                    throw new RequestWarningException(ErrorTypeEnum.WarningException, ExceptionCodeHelper.CannotEmptyField, ExceptionMessageHelper.CannotEmptyField("Name"));
                }
                #endregion

                var data = GetAllCachedData <ProductEntity>().ToList();
                #region Field Control
                if (data != null)
                {
                    if (data.Any(q => q.Name == dto.Name && q.ProductTypeId == dto.ProductTypeId))
                    {
                        throw new RequestWarningException(ErrorTypeEnum.WarningException, ExceptionCodeHelper.IsInUse, ExceptionMessageHelper.IsInUse("Name"));
                    }
                }
                #endregion

                var entity = dto.ConvertTo <ProductEntity>();
                var conn   = Db.CreateConnection(true);
                if (dto.Id > 0)
                {
                    entity.UpdateUser = RequestUserId;
                    entity.UpdateDate = DateTimeHelper.Now;
                    conn.Update(entity, Db._DbTransaction);
                    data.RemoveAt(data.FindIndex(q => q.Id == entity.Id));
                }
                else
                {
                    int Id = conn.Insert(entity, Db._DbTransaction).ToInt();
                    entity = conn.Get <ProductEntity>(Id);
                }
                data.Add(entity);
                FillCacheData(data);
                return(entity.ConvertTo <ProductDto>());
            }
            catch (KnownException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                Logger.AddLog(LogTypeEnum.Error, "ProductManager.CreateOrUpdateProduct", RequestUserId, ex.Message, dto.ToJson(), ex);
                throw new KnownException(ErrorTypeEnum.UnexpectedExeption, ex.Message, ex);
            }
        }