Example #1
0
        public override async Task <DeleteResult.IDeleteResult> Work(DeleteCmd Op, Unit state)
        {
            try
            {
                var entity = _ctx.Entry(Op.Item);
                switch (entity.State)
                {
                case EntityState.Unchanged:
                    entity.State = EntityState.Deleted;
                    await _ctx.SaveChangesAsync();

                    return(new DeleteResult.Successful(entity.Entity));

                case EntityState.Added:
                    _ctx.Remove(entity);
                    return(new DeleteResult.Successful(entity.Entity));

                default:
                    return(new DeleteResult.Failed($"Entity is in an incorrect state. Expected Added or Unchanged. Got {entity.State.ToString()}"));
                }
            }
            catch (Exception ex)
            {
                //log exception
                return(new DeleteResult.Failed(ex.ToString()));
            }
        }
        public override async Task <IAddOrUpdateResult> Work(AddOrUpdateCmd Op, Unit state)
        {
            try
            {
                var entity = _ctx.Entry(Op.Item);
                switch (entity.State)
                {
                case EntityState.Detached:
                    _ctx.Add(Op.Item);
                    break;

                case EntityState.Unchanged:
                    _ctx.Attach(Op.Item);
                    break;

                default:
                    return(new Failed($"Entity is in an incorrect state. Expected Detached or Unchanged. Got {entity.State.ToString()}"));
                }
                await _ctx.SaveChangesAsync();

                return(new Successful(entity.Entity));
            }
            catch (Exception ex)
            {
                return(new Failed(ex.ToString()));
            }
        }
        public override async Task <IUpdateEntityResult> Work(UpdateEntityCmd Op, Unit state)
        {
            try
            {
                var entity = _context.Entry(Op.Item);

                _context.Update(Op.Item);
                await _context.SaveChangesAsync();

                return(new Success(entity.Entity));
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
                return(new Failed(exp.ToString()));
            }
        }
        public override async Task <IAddEntityResult> Work(AddEntityCmd Op, Unit state)
        {
            try
            {
                var entity = _context.Entry(Op.Item);
                if (entity.State == Microsoft.EntityFrameworkCore.EntityState.Detached)
                {
                    _context.Add(Op.Item);
                    await _context.SaveChangesAsync();

                    return(new Success(entity.Entity));
                }
                else
                {
                    return(new Failed("Incorrect entity state! Expected Detached, got" + entity.State));
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
                return(new Failed(exp.ToString()));
            }
        }
Example #5
0
        public override async Task <IDeteleEntityResult> Work(DeleteEntityCmd Op, Unit state)
        {
            try
            {
                var entity = _context.Entry(Op.Item);
                //if (entity.State == Microsoft.EntityFrameworkCore.EntityState.De)
                // {
                _context.Remove(Op.Item);
                await _context.SaveChangesAsync();

                return(new Success());
                //}
                //else
                // {
                //     return new Failed("Incorrect entity state! Expected Detached, got" + entity.State);
                //}
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.ToString());
                return(new Failed(exp.ToString()));
            }
        }
        public override async Task <IAddOrUpdateResult> Work(AddOrUpdateCmd Op, Unit state)
        {
            var(valid, validationResults) = Op.Validate();
            string validationMessage = "";

            validationResults.ForEach(x => validationMessage += x.ErrorMessage);

            if (!valid)
            {
                return(new Failed(validationMessage));
            }

            try
            {
                var entity = _ctx.Entry(Op.Item);
                switch (entity.State)
                {
                case EntityState.Detached:
                    _ctx.Add(Op.Item);
                    break;

                case EntityState.Unchanged:
                    _ctx.Update(Op.Item);
                    break;

                default:
                    return(new Failed($"Entity is in an incorrect state. Expected Detached or Unchanged. Got {entity.State.ToString()}"));
                }
                await _ctx.SaveChangesAsync();

                return(new Successful(entity.Entity));
            }
            catch (Exception ex)
            {
                return(new Failed(ex.ToString()));
            }
        }