Exemplo n.º 1
0
 public Yoyos.Dto.Yoyo CreateYoyo(Yoyo yoyo)
 {
     return new Yoyos.Dto.Yoyo()
     {
         Id = yoyo.Id,
         Name = yoyo.Name,
         Description = yoyo.Description,
         Created = yoyo.Created
     };
 }
Exemplo n.º 2
0
        public RepositoryActionResult<Yoyo> UpdateYoyo(Yoyo y)
        {
            try
            {

                // you can only update when an expense already exists for this id

                var existingYoyo = _context.Yoyos.FirstOrDefault(yoyo => yoyo.Id == y.Id);

                if (existingYoyo == null)
                {
                    return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.NotFound);
                }

                // change the original entity status to detached; otherwise, we get an error on attach
                // as the entity is already in the dbSet

                // set original entity state to detached
                _context.Entry(existingYoyo).State = EntityState.Detached;

                // attach & save
                _context.Yoyos.Attach(y);

                // set the updated entity state to modified, so it gets updated.
                _context.Entry(y).State = EntityState.Modified;


                var result = _context.SaveChanges();

                if (result > 0)
                {
                    return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.Updated);
                }
                else
                {
                    return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.NothingModified, null);
                }
            }
            catch (Exception ex)
            {
                return new RepositoryActionResult<Yoyo>(null, RepositoryActionStatus.Error, ex);
            }
        }
Exemplo n.º 3
0
        public RepositoryActionResult<Yoyo> InsertYoyo(Yoyo y)
        {
            try
            {
                _context.Yoyos.Add(y);
                var result = _context.SaveChanges();

                if (result > 0)
                {
                    return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.Created);
                }
                else
                {
                    return new RepositoryActionResult<Yoyo>(y, RepositoryActionStatus.NothingModified, null);
                }

            }
            catch (Exception ex)
            {
                return new RepositoryActionResult<Yoyo>(null, RepositoryActionStatus.Error, ex);
            }
        }