예제 #1
0
        public void UpdateTest()
        {
            DataContext dataContext = new DataContext {
                SProducts = new List <SProduct>()
            };
            SProductRepository productRepository = new SProductRepository(dataContext);

            productRepository.Add(new SProduct
            {
                Id         = 1,
                MinimalAge = 19,
                Author     = "21Studios",
                Name       = "Game 1",
                Price      = 20.4f
            });
            string newAuthor = "TSA";

            SProduct product2 = new SProduct
            {
                Id         = 1,
                MinimalAge = 19,
                Author     = newAuthor,
                Name       = "Game 1",
                Price      = 20.4f
            };

            productRepository.Update(1, product2);

            Assert.AreEqual(newAuthor, productRepository.Get(1).Author);
        }
예제 #2
0
        public string Resolve(string message)
        {
            if (message.Contains("GetUser"))
            {
                Log("[Server]: Resolving GetUser request");
                string[] parameters = message.Split(':');
                SClient  user       = ClientService.GetUser(Int32.Parse(parameters[1])).Result;

                MemoryStream stream = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SClient));
                serializer.WriteObject(stream, user);

                stream.Position = 0;
                StreamReader sr     = new StreamReader(stream);
                string       output = sr.ReadToEnd();

                return(output);
            }
            else if (message.Contains("GetProduct"))
            {
                Log("[Server]: Resolving GetProduct request");
                string[] parameters = message.Split(':');
                SProduct product    = ProductService.GetProduct(Int32.Parse(parameters[1]));

                MemoryStream stream = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SProduct));
                serializer.WriteObject(stream, product);

                byte[] json = stream.ToArray();
                stream.Close();
                string result = Encoding.UTF8.GetString(json, 0, json.Length);
                return(Encoding.UTF8.GetString(json, 0, json.Length));
            }
            else if (message.Contains("GetAllProducts"))
            {
                Log("[Server]: Resolving GetAllProducts request");
                IEnumerable <SProduct> products = ProductService.GetProducts();

                List <SProduct> serializationList = new List <SProduct>();

                foreach (SProduct prod in products)
                {
                    serializationList.Add(prod);
                }

                MemoryStream stream = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List <SProduct>));
                serializer.WriteObject(stream, serializationList);

                byte[] json = stream.ToArray();
                stream.Close();
                string result = Encoding.UTF8.GetString(json, 0, json.Length);

                return(Encoding.UTF8.GetString(json, 0, json.Length));
            }
            else
            {
                return("Can't process given message");
            }
        }
예제 #3
0
        public static ProductDTO MapProduct(SProduct product)
        {
            ProductDTO productDTO = new ProductDTO
            {
                Id         = product.Id,
                Author     = product.Author,
                Name       = product.Name,
                MinimalAge = product.MinimalAge,
                Price      = product.Price
            };

            return(productDTO);
        }
예제 #4
0
        public static SProduct MapProduct(Product product)
        {
            SProduct sproduct = new SProduct
            {
                Id         = product.Id,
                Author     = product.Author,
                Name       = product.Name,
                MinimalAge = DateTime.Now.Year - product.AllowedFromDate.Year,
                Price      = product.Price
            };

            return(sproduct);
        }
예제 #5
0
        public SProduct Update(int id, SProduct entity)
        {
            lock (m_SyncObject)
            {
                SProduct product = _dataContext.SProducts.FirstOrDefault(c => c.Id == id);

                product.Author     = entity.Author;
                product.Name       = entity.Name;
                product.Price      = entity.Price;
                product.MinimalAge = entity.MinimalAge;

                return(product);
            }
        }
예제 #6
0
        private void ParseMessege(string message)
        {
            MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(message));

            try
            {
                SProduct deserializedObject           = new SProduct();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(deserializedObject.GetType());
                deserializedObject = serializer.ReadObject(memoryStream) as SProduct;

                DbContext.Instance.SProducts.Add(deserializedObject);
            }
            catch (SerializationException e)
            {
                try
                {
                    List <SProduct>            deserializedObject = new List <SProduct>();
                    DataContractJsonSerializer serializer         = new DataContractJsonSerializer(typeof(List <SProduct>));

                    memoryStream.Position = 0;
                    deserializedObject    = serializer.ReadObject(memoryStream) as List <SProduct>;

                    foreach (SProduct prod in deserializedObject)
                    {
                        DbContext.Instance.SProducts.Add(prod);
                    }
                }
                catch (SerializationException ee)
                {
                    try
                    {
                        SClient deserializedObject            = new SClient();
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(deserializedObject.GetType());
                        deserializedObject = serializer.ReadObject(memoryStream) as SClient;

                        DbContext.Instance.SClients.Add(deserializedObject);
                    }
                    catch (SerializationException eeee)
                    {
                        return;
                    }
                }
            }

            memoryStream.Close();
        }
예제 #7
0
        public void Delete(int id)
        {
            SProduct product = _dataContext.SProducts.FirstOrDefault(c => c.Id == id);

            _dataContext.SProducts.Remove(product);
        }
예제 #8
0
 public SProduct Add(SProduct entity)
 {
     _dataContext.SProducts.Add(entity);
     return(entity);
 }
예제 #9
0
        public async Task <ProductDTO> GetProduct(int id)
        {
            SProduct product = _sproductRepository.Get(id);

            return(Mappings.MapProduct(product));
        }