Пример #1
0
 public void CopyFrom(WarningViewModel itemViewModel, DBUnitOfWork db)
 {
     Message = itemViewModel.Message;
     if (itemViewModel.ItemEntityId != null)
     {
         ItemEntityId = itemViewModel.ItemEntityId.Value;
     }
     else
     {
         throw new Exception("This Warning doesn't have an associated Item!");
     }
 }
Пример #2
0
 public AuthRequest GetAuthRequestFromToken(string token)
 {
     using (var db = new DBUnitOfWork())
     {
         var tokenModel = db.Repository<TokenModel>().Find(x => x.Token.Equals(token));
         return new AuthRequest()
         {
             User = tokenModel.Username,
             Password = tokenModel.Password
         };
     }
 }
Пример #3
0
 public void InvalidateTokensForUser(string userName, string token)
 {
     if (token == null)
     {
         return;
     }
     using (var db = new DBUnitOfWork())
     {
         var tokens = db.Repository<TokenModel>().FindAll(x => x.Username.Equals(userName));
         foreach (var oldToken in tokens.Where(x => !x.Token.Equals(token)))
         {
             db.Repository<TokenModel>().Delete(oldToken);
         }
     }
 }
Пример #4
0
        public static void UpdateWarningForItem(Item item)
        {
            var warningEnabled = item.Quantity < item.MinQuantity;
            var shouldUpdateExisting = false;
            try
            {
                using (var db = new DBUnitOfWork())
                {
                    var existing = db.Repository<Warning>().Find(x => x.ItemEntityId.Equals(item.EntityId));
                    if (warningEnabled)
                    {
                        if (existing == null)
                        {
                            var warning = new Warning
                            {
                                ItemEntityId = item.EntityId,
                                Message = "Warning, [ " + item.Name + " ] is insuficiently stocked, need [ " + (item.MinQuantity - item.Quantity) + " ] more!"
                            };

                            item.Status = "Warning";
                            db.Repository<Warning>().Add(warning);
                            shouldUpdateExisting = true;
                        }
                    }
                    else
                    {
                        if (existing != null)
                        {
                            item.Status = string.Empty;
                            db.Repository<Warning>().Delete(existing.EntityId);
                            shouldUpdateExisting = true;
                        }
                    }
                }

                if (shouldUpdateExisting)
                {
                    using (var db = new DBUnitOfWork())
                    {
                        db.Repository<Item>().Update(item, item.EntityId);
                    }
                }
            }
            catch (Exception)
            {
                
            }
        }
Пример #5
0
 public HttpResponseMessage Post([FromBody] CategoryViewModel categoryViewModel)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             Category category = new Category();
             category.CopyFrom(categoryViewModel, db);
             db.Repository<Category>().Add(category);
             return Request.CreateResponse(HttpStatusCode.Created);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #6
0
 public HttpResponseMessage Post([FromBody] LocationViewModel locationViewModel)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             Location location = new Location();
             location.CopyFrom(locationViewModel,db);
             db.Repository<Location>().Add(location);
             return Request.CreateResponse(HttpStatusCode.Created);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #7
0
 public void CopyFrom(CategoryViewModel categoryViewModel, DBUnitOfWork db)
 {
     Name = categoryViewModel.Name;
     Description = categoryViewModel.Description;
     Items = new List<Item>();
     if (categoryViewModel.Items != null)
     {
         foreach (var itemId in categoryViewModel.Items)
         {
             var existing = db.Repository<Item>().Get(itemId);
             if (existing != null)
             {
                 Items.Add(existing);
             }
         }
     }
 }
Пример #8
0
        static void Main(string[] args)
        {
            var db=new DBUnitOfWork();

            var language=new Language
            {
                Name="Englishhh",
                LanguageId=1
            };

            db.Languages.Add(language);
            db.SaveChanges();

            foreach (var item in db.Languages.All())
            {
                Console.WriteLine(item);
            }
        }
Пример #9
0
 public HttpResponseMessage Post([FromBody] ItemViewModel itemViewModel)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             Item item = new Item();
             item.CopyFrom(itemViewModel,db);
             db.Repository<Item>().Add(item);
             WarningController.UpdateWarningForItem(item);
             return Request.CreateResponse(HttpStatusCode.Created);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #10
0
 public HttpResponseMessage Get([FromODataUri] string key)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             foreach (var item in db.Repository<Item>().GetAll())
             {
                 WarningController.UpdateWarningForItem(item);
             }
             return Request.CreateResponse(HttpStatusCode.OK);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #11
0
 public TokenModel GenerateAndRegisterTokenForUserWithRights(string username, string password, Role[] roles)
 {
     var tokenModel = new TokenModel
     {
         Roles = string.Join(",", roles),
         Username = username,
         ExpirationDateTime = DateTime.Now.AddDays(1),
         Token =
             Convert.ToBase64String(
                 Encoding.Default.GetBytes(username + "_" + Guid.NewGuid())),
         Password = password
     };
     using (var db = new DBUnitOfWork())
     {
         db.Repository<TokenModel>().Add(tokenModel);
     }
     return tokenModel;
 }
Пример #12
0
 public void CopyFrom(ItemViewModel itemViewModel, DBUnitOfWork db)
 {
     //Id = itemViewModel.Id;
     Name = itemViewModel.Name;
     Description = itemViewModel.Description;
     Status = itemViewModel.Status;
     
     Location = db.Repository<Location>().Get(LocationId);
     if (itemViewModel.LocationId != null)
     {
         LocationId = itemViewModel.LocationId.Value;
     }
     else
     {
         //LocationId = itemViewModel.LocationId ?? new Guid("803ad3c6-cd87-4ad8-9a26-3675f4999f42");
         var defaultLocation = db.Repository<Location>().Find(x => x.Name == "Unknown");
         if (defaultLocation != null)
         {
             LocationId = defaultLocation.EntityId;
         }
         else
         {
             throw new Exception("You don't have the 'Unknown' location.");
         }
     }
     Quantity = itemViewModel.Quantity;
     MinQuantity = itemViewModel.MinQuantity;
     Relevance = itemViewModel.Relevance;
     Price = itemViewModel.Price;
     Picture = itemViewModel.Picture;
     
     Categories = new List<Category>();
     if (itemViewModel.Categories != null)
     {
         foreach (var catId in itemViewModel.Categories)
         {
             var existing = db.Repository<Category>().Get(catId);
             if (existing != null)
             {
                 Categories.Add(existing);
             }
         }
     }
 }
Пример #13
0
        public void CopyFrom(LocationViewModel locationViewModel, DBUnitOfWork db)
        {
            //Id = locationViewModel.Id;
            Name = locationViewModel.Name;
            Description = locationViewModel.Description;

            Items = new List<Item>();
            if (locationViewModel.Items != null)
            {
                foreach (var itemId in locationViewModel.Items)
                {
                    var existing = db.Repository<Item>().Get(itemId);
                    if (existing != null)
                    {
                        Items.Add(existing);
                    }
                } 
            }
        }
Пример #14
0
 public HttpResponseMessage Put([FromODataUri] string key, [FromBody] LocationViewModel locationViewModel)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             Location location = new Location();
             location.CopyFrom(locationViewModel, db);
             location.EntityId = new Guid(key);
             if (db.Repository<Location>().Update(location, location.EntityId) != null)
                 return Request.CreateResponse(HttpStatusCode.OK);
             else
                 return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #15
0
 public HttpResponseMessage Put([FromODataUri] string key, [FromBody] CategoryViewModel categoryViewModel)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             Category category = new Category();
             category.CopyFrom(categoryViewModel, db);
             category.EntityId = new Guid(key);
             if (db.Repository<Category>().Update(category, category.EntityId) != null)
                 return Request.CreateResponse(HttpStatusCode.OK);
             else
                 return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #16
0
 public HttpResponseMessage GetItem()
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             List<ItemViewModel> itemViewModelList = new List<ItemViewModel>();
             var itemList = db.Repository<Item>().GetAll();
             foreach (var item in itemList)
             {
                 ItemViewModel itemViewModel = new ItemViewModel();
                 itemViewModel.CopyFrom(item);
                 itemViewModelList.Add(itemViewModel);
             }
             return Request.CreateResponse(HttpStatusCode.OK, itemViewModelList);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #17
0
 public HttpResponseMessage Get()
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             var warningViewModels = new List<WarningViewModel>();
             var warningModels = db.Repository<Warning>().GetAll();
             foreach (var warning in warningModels)
             {
                 var warningViewModel = new WarningViewModel();
                 warningViewModel.CopyFrom(warning);
                 warningViewModels.Add(warningViewModel);
             }
             return Request.CreateResponse(HttpStatusCode.OK, warningViewModels); ;
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #18
0
 public HttpResponseMessage Get()
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             List<CategoryViewModel> categoryViewModelList = new List<CategoryViewModel>();
             var categoryList = db.Repository<Category>().GetAll();
             foreach (var category in categoryList)
             {
                 CategoryViewModel categoryViewModel = new CategoryViewModel();
                 categoryViewModel.CopyFrom(category);
                 categoryViewModelList.Add(categoryViewModel);
             }
             return Request.CreateResponse(HttpStatusCode.OK, categoryViewModelList);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #19
0
 public HttpResponseMessage Get()
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             List<LocationViewModel> locationViewModelList = new List<LocationViewModel>();
             var locationList = db.Repository<Location>().GetAll();
             foreach (var location in locationList)
             {
                 LocationViewModel locationViewModel = new LocationViewModel();
                 locationViewModel.CopyFrom(location);
                 locationViewModelList.Add(locationViewModel);
             }
             return Request.CreateResponse(HttpStatusCode.OK, locationViewModelList);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #20
0
 public HttpResponseMessage Put([FromODataUri] string key, [FromBody] ItemViewModel itemViewModel)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             Item item = new Item();
             item.CopyFrom(itemViewModel, db);
             item.EntityId = new Guid(key);
             if (db.Repository<Item>().Update(item, item.EntityId) != null)
             {
                 WarningController.UpdateWarningForItem(item);
                 return Request.CreateResponse(HttpStatusCode.OK);
             }
             else
                 return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #21
0
 public bool IsTokenValid(string token, out Role[] role)
 {
     using (var db = new DBUnitOfWork())
     {
         var tokenModel = db.Repository<TokenModel>().Find(x => x.Token.Equals(token));
         if (tokenModel != null)
         {
             if (DateTime.Now > tokenModel.ExpirationDateTime.AddDays(1))
             {
                 db.Repository<TokenModel>().Delete(tokenModel);
             }
             else
             {
                 role =
                     tokenModel.Roles.Split(',')
                         .Select(roleStr => (Role) Enum.Parse(typeof (Role), roleStr))
                         .ToArray();
                 return true;
             }
         }
     }
     role = new[] {Role.None};
     return false;
 }
Пример #22
0
 public HttpResponseMessage Delete([FromODataUri] string key)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             var itemEntityId = new Guid(key);
             if (db.Repository<Item>().Delete(itemEntityId) == true)
             {
                 WarningController.DeleteWarningForItem(itemEntityId);
                 return Request.CreateResponse(HttpStatusCode.OK);
             }
             else
                 return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }
Пример #23
0
 public static void DeleteWarningForItem(Guid itemEntityId)
 {
     using (var db = new DBUnitOfWork())
     {
         var existing = db.Repository<Warning>().Find(x => x.ItemEntityId.Equals(itemEntityId));
         if (existing != null)
         {
             db.Repository<Warning>().Delete(existing);
         }
     }
 }
Пример #24
0
 public HttpResponseMessage Delete([FromODataUri] string key)
 {
     using (var db = new DBUnitOfWork())
     {
         try
         {
             if (db.Repository<Location>().Delete(new Guid(key)) == true)
                 return Request.CreateResponse(HttpStatusCode.OK);
             else
                 return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         catch (Exception)
         {
             return Request.CreateResponse(HttpStatusCode.InternalServerError);
         }
     }
 }