예제 #1
0
        private ThingDto buildDto(ThingEntity entity)
        {
            ThingDto dto = new ThingDto();

            dto.DefaultPrice = entity.DefaultPrice;
            dto.Name         = entity.Name;
            dto.Show         = entity.Show;
            dto.ThingId      = entity.ThingId;
            dto.Needed       = entity.Needed;
            dto.HouseholdId  = entity.HouseholdId;

            return(dto);
        }
예제 #2
0
        public ThingDto Create(ThingDto dto)
        {
            ThingEntity entity = new ThingEntity();

            entity.Name         = dto.Name;
            entity.HouseholdId  = dto.HouseholdId;
            entity.Show         = dto.Show;
            entity.Needed       = dto.Needed;
            entity.DefaultPrice = dto.DefaultPrice;

            context.Things.Add(entity);
            context.SaveChanges();

            return(buildDto(entity));
        }
예제 #3
0
        public ThingDto Delete(int id)
        {
            ThingEntity entity = context.Things.Find(id);

            if (entity != null)
            {
                ThingEntity retEntity = context.Things.Remove(entity);

                context.SaveChanges();

                return(buildDto(retEntity));
            }
            else
            {
                throw (new KeyNotFoundException());
            }
        }
예제 #4
0
        public ThingDto Update(ThingDto dto)
        {
            ThingEntity entity = context.Things.Find(dto.ThingId);

            if (entity != null)
            {
                entity.Show         = dto.Show;
                entity.Needed       = dto.Needed;
                entity.Name         = dto.Name;
                entity.DefaultPrice = dto.DefaultPrice;

                context.SaveChanges();

                return(buildDto(entity));
            }
            else
            {
                throw new KeyNotFoundException($"Thing with id [{dto.ThingId}] not found");
            }
        }
예제 #5
0
        /// <summary>
        /// Get thing DTO by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Thing Data Transfer Object</returns>
        /// <exception cref="KeyNotFoundException">Datbase entry not found</exception>
        public ThingDto GetById(int id)
        {
            ThingEntity entity = context.Things.Find(id);

            if (entity != null)
            {
                ThingDto dto = new ThingDto()
                {
                    ThingId      = entity.ThingId,
                    DefaultPrice = entity.DefaultPrice,
                    Name         = entity.Name,
                    Needed       = entity.Needed,
                    Show         = entity.Show,
                    HouseholdId  = entity.HouseholdId
                };
                return(dto);
            }
            else
            {
                //  Use Key not found Exception for when a resource is not found
                throw new KeyNotFoundException();
            }
        }