Beispiel #1
0
        public async Task AddFlattened()
        {
            var dto = new PersonDTOFlattened
            {
                Name          = "NewName",
                Surname       = "NewSurname",
                SpouseName    = "NewSpouseName",
                SpouseSurname = "NewSpouseSurname",
                Children      = new List <PersonDTOFlattened>()
                {
                    new PersonDTOFlattened
                    {
                        Name          = "NewNameChildren",
                        Surname       = "NewSurnameChildren",
                        SpouseName    = "NewSpouseNameChildren",
                        SpouseSurname = "NewSpouseSurnameChildren",
                    }
                },
                SpouseChildren = new List <PersonDTOFlattened>()
                {
                    new PersonDTOFlattened
                    {
                        Name          = "NewNameChildrenSpouse",
                        Surname       = "NewSurnameChildrenSpouse",
                        SpouseName    = "NewSpouseNameChildrenSpouse",
                        SpouseSurname = "NewSpouseSurnameChildrenSpouse",
                    }
                }
            };

            repository.Add <PersonDTOFlattened>(true, dto);
            await repository.SaveChanges();

            var id  = dto.Id.Value;
            var res = await repository.GetById <PersonDTOFlattened, int>(id);

            Assert.NotNull(res);
            Assert.Equal(res.Name, "NewName");
            Assert.Equal(res.Surname, "NewSurname");
            Assert.Equal(res.SpouseName, "NewSpouseName");
            Assert.Equal(res.SpouseSurname, "NewSpouseSurname");

            Assert.NotNull(res.Children);
            Assert.Equal(res.Children.Count(), 1);
            var child = res.Children.FirstOrDefault();

            Assert.Equal(child.Name, "NewNameChildren");
            Assert.Equal(child.Surname, "NewSurnameChildren");
            Assert.Null(child.SpouseName);
            Assert.Null(child.SpouseSurname);

            Assert.Equal(res.SpouseChildren.Count(), 1);
            child = res.SpouseChildren.FirstOrDefault();

            Assert.Equal(child.Name, "NewNameChildrenSpouse");
            Assert.Equal(child.Surname, "NewSurnameChildrenSpouse");
            Assert.Equal(child.SpouseName, "NewSpouseNameChildrenSpouse");
            Assert.Equal(child.SpouseSurname, "NewSpouseSurnameChildrenSpouse");
        }
        public void Add <T>(bool full, params T[] viewModel)
        {
            var prep = findMap <T>();

            if (prep == null)
            {
                repository.Add <T>(full, viewModel);
            }
            else
            {
                prep.Add(repository, context, full, viewModel);
            }
        }
Beispiel #3
0
        public static async Task Store <TEntity, TKey>(this ICRUDRepository <TEntity, TKey> repository, TEntity entity, TKey key) where TEntity : class
        {
            TEntity entityinDb = await repository.GetTracking(key);

            if (entityinDb == default(TEntity))
            {
                repository.Add(entity);
            }
            else
            {
                // syn with existing
                repository.SetValue(entityinDb, entity);
            }
        }
 private void btnSave_Click(object sender, RoutedEventArgs e)
 {
     if (dpDate.SelectedDate.HasValue &&
         cmbExpenseOrIncome.SelectedItem != null &&
         cmbCategory.SelectedItem != null &&
         decimal.TryParse(txtAmount.Text, out decimal amount) &&
         amount > 0
         )
     {
         Transaction newTransaction = new Transaction
         {
             UserId = SelectedUser.UserId,
             TransactionCategoryId = _selectedTransactionCategory.TransactionCategoryId,
             TransactionDate       = dpDate.SelectedDate.Value,
             Sum = amount
         };
         _transactionManager.Add(newTransaction);
         MessageBox.Show("Entry saved!");
         cmbExpenseOrIncome.SelectedItem = null;
         cmbCategory.SelectedItem        = null;
         txtAmount.Text        = string.Empty;
         cmbCategory.IsEnabled = false;
         btnSave_ClickHandler(sender, e);
     }
     else if (cmbExpenseOrIncome.SelectedItem == null)
     {
         MessageBox.Show("Expense/Income was not selected!");
     }
     else if (cmbCategory.SelectedItem == null)
     {
         MessageBox.Show("Category was not selected!");
     }
     else if (!decimal.TryParse(txtAmount.Text, out amount))
     {
         MessageBox.Show("Amount must be decimal number!");
     }
     else if (amount <= 0)
     {
         MessageBox.Show("Amount must be greater than 0!");
     }
     else
     {
         MessageBox.Show("Incorrect input!");
     }
 }
        protected async Task <HttpResponseMessage> Post(TObject item)
        {
            if (item == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Item is empty"));
            }
            else if (ModelState.IsValid)
            {
                item = await repository.Add(item);

                var response = Request.CreateResponse <TObject>(HttpStatusCode.Created, item);

                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Beispiel #6
0
        public async Task <IActionResult> PostAPost(string postContent, string postNotes, string[] postIngredientIds)
        {
            //Gets the current user
            AppUser currentUser = await _appUserManager.GetUserAsync(User);

            //Checks if Comment.Content is null
            if (string.IsNullOrWhiteSpace(postContent))
            {
                ModelState.AddModelError("Content", "Content is required.");
                return(Json(new { redirectToUrl = Url.Action("Profile", "User", new { id = currentUser.Id }) }));
            }

            //Creates new Post instance
            Post newPost = new()
            {
                Content       = postContent,
                Notes         = postNotes,
                AppUserId     = currentUser.Id,
                NumberOfYums  = 0,
                TimeOfPosting = DateTime.Now
            };
            //Adds newPost instance to database
            Post addedPost = await _postRepository.Add(newPost);

            //Creates and adds post_Ingredient instances to database
            //Very bad performace due to constantly SaveChanges() for each object, should AddRange() instead
            foreach (var ingredientId in postIngredientIds)
            {
                var post_Ingredient = new Post_Ingredient
                {
                    PostId       = addedPost.Id,
                    AppUserId    = addedPost.AppUserId,
                    IngredientId = int.Parse(ingredientId)
                };

                await _post_IngredientRepository.Add(post_Ingredient);
            }

            return(Json(new { redirectToUrl = Url.Action("SinglePost", "User", new { id = addedPost.Id }) }));
        }
        public async Task Add()
        {
            var dto = new PersonDTOFlattenedAuto()
            {
                Name          = "RootNew",
                Surname       = "SurnameNew",
                SpouseName    = "SpouseNameNew",
                SpouseSurname = "SpouseSurnameNew"
            };

            repository.Add(true, dto);
            await repository.SaveChanges();

            Assert.NotNull(dto.Id);
            var id    = dto.Id.Value;
            var added = await repository.GetById <PersonDTOFlattenedAuto, int>(id);

            Assert.NotNull(added);

            Assert.Equal(added.Name, "RootNew");
            Assert.Equal(added.Surname, "SurnameNew");
            Assert.Equal(added.SpouseName, "SpouseNameNew");
            Assert.Equal(added.SpouseSurname, "SpouseSurnameNew");
        }