Ejemplo n.º 1
0
        public async Task <IActionResult> Post(string dictype, [FromBody] object dto)
        {
            var resolver = new DicTypeResolver();
            var obj      = JsonConvert.DeserializeObject(value: dto.ToString(), type: resolver.Resolve(dictype));

            var dictionaries = _context.Set(resolver.Resolve(dictype)) as IQueryable <IDictionaryEntity <int> >;
            var id           = 1;
            var dictionary   = dictionaries?.SingleOrDefault(d => d.Id == id);

            MapAllowedProperties(obj, dictionary);

            _context.Update(dictionary);

            await _context.SaveChangesAsync();

            return(Ok(obj));


            void MapAllowedProperties(object source, object dest)
            {
                string[] ignored = { "Id", "Code", "ExternalId", "DateCreate", "DateUpdate" };
                bool Pred(PropertyInfo p) => !ignored.Contains(p.Name);

                var sourceProps = source.GetType()
                                  .GetProperties()
                                  .Where(Pred)
                                  .OrderBy(p => p.Name)
                                  .ToList();

                var destProps = source.GetType()
                                .GetProperties()
                                .Where(Pred)
                                .OrderBy(p => p.Name)
                                .ToList();

                for (var i = 0; i < destProps.Count; i++)
                {
                    sourceProps[i].SetValue(dest, sourceProps[i].GetValue(source));
                }
            }
        }
Ejemplo n.º 2
0
        private int GetNextCount(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentNullException(nameof(code));
            }

            var           currentCounter = _context.SystemCounter.SingleOrDefault(c => c.Code.Equals(code));
            SystemCounter nextCounter;

            if (currentCounter != null)
            {
                currentCounter.Count = ++currentCounter.Count;
                nextCounter          = _context.Update(currentCounter).Entity;
            }
            else
            {
                nextCounter = _context.Add(new SystemCounter {
                    Code = code, Count = 1
                }).Entity;
            }

            return(nextCounter.Count);
        }