Beispiel #1
0
        public Result InsertEntity <T>(T item, bool ignoreValidation = false) where T : class
        {
            using (DBContext context = new HotelReservationsContext())
            {
                if (ContainsEntity(item, context))
                {
                    return(new Result(ResultType.ALREADY_IN_DATABASE));
                }
                // validate item if needed
                if (!ignoreValidation)
                {
                    Result result = Validate(item);
                    if (result.type != ResultType.SUCCESS)
                    {
                        return(result);
                    }
                }

                // add item
                context.Add(item);
                context.SaveChanges();
                OnDBChange?.Invoke(context, typeof(T));
            }
            return(new Result(ResultType.SUCCESS));
        }
Beispiel #2
0
 public void RemoveEntity <T>(T entity)
 {
     using (DBContext context = new HotelReservationsContext())
     {
         context.Remove(entity);
         context.SaveChanges();
         OnDBChange?.Invoke(context, typeof(T));
     }
 }
Beispiel #3
0
        public Result EditEntity <T>(T entity) where T : class
        {
            using (DBContext context = new HotelReservationsContext())
            {
                if (!ContainsEntity(entity, context))
                {
                    return(new Result(ResultType.NOT_IN_DATABASE));
                }

                Result result = Validate(entity);
                if (result.type != ResultType.SUCCESS)
                {
                    return(result);
                }
                context.Update(entity);
                context.SaveChanges();
                OnDBChange?.Invoke(context, typeof(T));
            }

            return(new Result(ResultType.SUCCESS));
        }