Beispiel #1
0
        public async Task EditGetShoudReturnsRedirectWhenRecordIdIsInvalid()
        {
            // Arrange
            this.FillDatabase();

            this.editModel = new EditRecordInputModel
            {
                Id          = null,
                OldAmount   = -5,
                Amount      = -6.50m,
                WalletId    = 5,
                CategoryId  = 5,
                Type        = ViewModels.Records.Enums.RecordTypeInputModel.Income,
                Description = "edited record description",
                CreatedOn   = DateTime.UtcNow,
                WalletName  = "Holiday Wallet",
            };

            // Act
            var result = await this.controller.Edit(this.editModel);

            // Assert
            var editedRecord = this.db.Records.FirstOrDefault(r => r.Id == "record1");

            var redirectResult = Assert.IsType <RedirectResult>(result);

            Assert.Equal("H", redirectResult.Url[1].ToString());
        }
Beispiel #2
0
        public async Task EditPostShoudEditRecordSuccessfully()
        {
            // Arrange
            this.FillDatabase();

            this.editModel = new EditRecordInputModel
            {
                Id          = "record1",
                OldAmount   = -5,
                Amount      = -6.50m,
                WalletId    = 5,
                CategoryId  = 5,
                Type        = ViewModels.Records.Enums.RecordTypeInputModel.Income,
                Description = "edited record description",
                CreatedOn   = DateTime.UtcNow,
                WalletName  = "Holiday Wallet",
            };

            // Act
            var result = await this.controller.Edit(this.editModel);

            // Assert
            var editedRecord = this.db.Records.FirstOrDefault(r => r.Id == "record1");

            var viewResult = Assert.IsType <RedirectResult>(result);

            Assert.Equal("edited record description", editedRecord.Description);
            Assert.Equal(RecordType.Income, editedRecord.Type);
            Assert.Equal(5, editedRecord.CategoryId);
            Assert.Equal(6.50m, editedRecord.Amount);
        }
Beispiel #3
0
        public async Task <ActionResult> Edit(EditRecordInputModel input)
        {
            try
            {
                var user = await this.userManager.GetUserAsync(this.User);

                if (!this.ModelState.IsValid)
                {
                    if (!await this.recordsService.IsUserOwnRecordAsync(user.Id, input.Id))
                    {
                        return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForViewOrEditRecord}"));
                    }

                    var recordDto = await this.recordsService.GetRecordWithAllCategories(input.Id, input.WalletId);

                    EditRecordViewModel model = new EditRecordViewModel()
                    {
                        Id          = recordDto.Id,
                        Description = recordDto.Description,
                        ModifiedOn  = recordDto.ModifiedOn,
                        CreatedOn   = recordDto.CreatedOn,
                        Type        = Enum.Parse <RecordTypeInputModel>(recordDto.Type.ToString()),
                        WalletId    = input.WalletId,
                        Amount      = recordDto.Amount,
                        OldAmount   = 0,
                        WalletName  = recordDto.WalletName,
                        CategoryId  = recordDto.CategoryId,
                        Categories  = recordDto.Categories.Select(c => new CategoryNameIdViewModel
                        {
                            Id         = c.Id,
                            Name       = c.Name,
                            WalletName = c.WalletName,
                        })
                                      .ToList(),
                    };

                    return(this.View(model));
                }

                if (!await this.recordsService.IsUserOwnRecordAsync(user.Id, input.Id))
                {
                    return(this.Redirect($"/Home/Error?message={GlobalConstants.NoPermissionForViewOrEditRecord}"));
                }

                await this.recordsService
                .UpdateRecord(
                    input.Id,
                    input.CategoryId,
                    input.WalletId,
                    input.Description,
                    input.OldAmount,
                    input.Amount,
                    input.Type.ToString(),
                    input.CreatedOn.HasValue?input.CreatedOn.Value : DateTime.UtcNow);

                return(this.Redirect($"/Wallets/Records/{input.WalletId}"));
            }
            catch (Exception ex)
            {
                return(this.Redirect($"/Home/Error?message={ex.Message}"));
            }
        }