Ejemplo n.º 1
0
        public async Task <HeroItem> CreateAsync(HeroItem item)
        {
            HeroesEntity entity = item;

            if (default(HeroesEntity) != entity)
            {
                if (ObjectId.Empty == entity.Id)
                {
                    entity.Id    = ObjectId.GenerateNewId();
                    entity.EmpNo = (await _context.HeroesEntityCollection.Find(x => true)
                                    .SortByDescending(a => a.EmpNo)
                                    .Project(x => x.EmpNo)
                                    .FirstAsync()) + 1;
                }

                await _context.HeroesEntityCollection.InsertOneAsync(entity);

                return(entity);
            }

            return(null);
        }
Ejemplo n.º 2
0
        public async Task <HeroItem> UpdateAsync(HeroItem item)
        {
            HeroesEntity entity = item;

            var filter = Builders <HeroesEntity> .Filter.Eq(x => x.Id, entity.Id);

            var update = Builders <HeroesEntity> .Update
                         .Set(x => x.Version, DateTime.UtcNow.Ticks);

            if (!string.IsNullOrEmpty(item.Name))
            {
                update = update.Set(x => x.Name, item.Name);
            }

            if (!string.IsNullOrEmpty(item.City))
            {
                update = update.Set(x => x.City, item.City);
            }

            var ret = await _context.HeroesEntityCollection.UpdateOneAsync(filter, update);

            return(entity);
        }