Пример #1
0
        private void ValidateSaveModel(LeadSaveModel candidate)
        {
            if (candidate is null)
            {
                throw new ArgumentNullException(nameof(candidate), "cannot be null");
            }

            if (string.IsNullOrWhiteSpace(candidate.Name))
            {
                throw new ArgumentException("Name cannot be empty", nameof(candidate.Name));
            }

            if (string.IsNullOrWhiteSpace(candidate.PinCode))
            {
                throw new ArgumentException("PinCode cannot be empty", nameof(candidate.PinCode));
            }

            if (string.IsNullOrWhiteSpace(candidate.Address))
            {
                throw new ArgumentException("Address cannot be empty", nameof(candidate.Address));
            }

            candidate.Name    = candidate.Name.Trim();
            candidate.PinCode = candidate.PinCode.Trim();
            candidate.Address = candidate.Address.Trim();
        }
Пример #2
0
        public async Task <ActionResult <LeadsSaveSuccessModel> > Post([FromBody] LeadsSaveViewModel candidate)
        {
            if (!candidate.SubAreaId.HasValue)
            {
                return(this.BadRequest(new ErrorViewModel("SubArea must have valid value")));
            }
            try
            {
                var leadSaveModel = new LeadSaveModel
                {
                    Name         = candidate.Name,
                    Address      = candidate.Address,
                    PinCode      = candidate.PinCode,
                    MobileNumber = candidate.MobileNumber,
                    Email        = candidate.Email,
                    SubAreaId    = candidate.SubAreaId.Value
                };

                var result = await leadsService.Save(leadSaveModel);

                return(this.Ok(new LeadsSaveSuccessModel(result)));
            }
            catch (Exception e)
            {
                return(this.BadRequest(new ErrorViewModel(e.Message)));
            }
        }
Пример #3
0
        public async Task <Guid> Save(LeadSaveModel lead)
        {
            ValidateSaveModel(lead);
            await ValidateSubArea(lead.SubAreaId, lead.PinCode)
            .ConfigureAwait(false);

            return(await this.leadsDb.Save(lead)
                   .ConfigureAwait(false));
        }
Пример #4
0
        public async void WhenValidSaveModelIsSaved_DbIsCalled()
        {
            subAreasMock.GetByIdReturn = new SubAreaViewModel {
                PinCode = "123"
            };
            var saveModel = new LeadSaveModel
            {
                Name = "name", Address = "addr", Email = "*****@*****.**", MobileNumber = "12345", PinCode = "123", SubAreaId = 1
            };

            leadsMock.SaveReturn = Guid.NewGuid();

            var resultLead = await this.leadsService.Save(saveModel).ConfigureAwait(false);

            Assert.True(leadsMock.IsSaveCalled);
        }
Пример #5
0
        public async void WhenSaveModelWithWhitespacesIsSaved_ValidationFixesIt()
        {
            subAreasMock.GetByIdReturn = new SubAreaViewModel {
                PinCode = "1234"
            };
            var saveModel = new LeadSaveModel
            {
                Name = " name  ", Address = "  addr ", Email = "*****@*****.**", MobileNumber = "12345", PinCode = "   1234 \t", SubAreaId = 1
            };

            await this.leadsService.Save(saveModel).ConfigureAwait(false);

            Assert.Equal("name", saveModel.Name);
            Assert.Equal("addr", saveModel.Address);
            Assert.Equal("1234", saveModel.PinCode);
        }
Пример #6
0
        public async void WhenSaveModelWithoutPinCodeIsSaved_ValidationExceptionIsThrown()
        {
            var saveModel = new LeadSaveModel
            {
                Name = "name", Address = "addr", Email = "*****@*****.**", MobileNumber = "12345", PinCode = null, SubAreaId = 1
            };
            await Assert.ThrowsAsync <ArgumentException>(
                async() => await this.leadsService.Save(saveModel).ConfigureAwait(false));

            saveModel.PinCode = "";
            await Assert.ThrowsAsync <ArgumentException>(
                async() => await this.leadsService.Save(saveModel).ConfigureAwait(false));

            saveModel.PinCode = " ";
            await Assert.ThrowsAsync <ArgumentException>(
                async() => await this.leadsService.Save(saveModel).ConfigureAwait(false));
        }
Пример #7
0
        public async Task <Guid> Save(LeadSaveModel lead)
        {
            var leadModel = new Lead
            {
                Name         = lead.Name,
                PinCode      = lead.PinCode,
                Address      = lead.Address,
                SubAreaId    = lead.SubAreaId,
                Email        = lead.Email,
                MobileNumber = lead.MobileNumber
            };

            await dbContext.Leads.AddAsync(leadModel);

            await dbContext.SaveChangesAsync();

            return(leadModel.Id);
        }
Пример #8
0
        public async void WhenInvalidPinCodeSubAreaIdCombinationIsSaved_ThrowException()
        {
            var saveModel = new LeadSaveModel
            {
                Name = "name", Address = "addr", Email = "*****@*****.**", MobileNumber = "12345", PinCode = "1234", SubAreaId = 1
            };

            subAreasMock.GetByIdReturn = null;

            await Assert.ThrowsAsync <ArgumentException>(
                async() => await this.leadsService.Save(saveModel).ConfigureAwait(false));

            subAreasMock.GetByIdReturn = new SubAreaViewModel {
                PinCode = "5678"
            };

            await Assert.ThrowsAsync <ArgumentException>(
                async() => await this.leadsService.Save(saveModel).ConfigureAwait(false));
        }
Пример #9
0
        public async Task <Guid> Save(LeadSaveModel lead)
        {
            var id      = Guid.NewGuid();
            var jsonObj = new {
                Id           = id,
                Address      = lead.Address,
                Email        = lead.Email,
                MobileNumber = lead.MobileNumber,
                Name         = lead.Name,
                PinCode      = lead.PinCode,
                SubAreaId    = lead.SubAreaId
            };
            var json = JsonConvert.SerializeObject(jsonObj);

            var filePath = path + "\\" + id + ".json";
            var file     = new FileInfo(filePath);

            file.Directory?.Create(); // If the directory already exists, this method does nothing.
            await File.WriteAllTextAsync(filePath, json);

            return(id);
        }
Пример #10
0
 public Task <Guid> Save(LeadSaveModel lead)
 {
     IsSaveCalled = true;
     return(Task.FromResult <Guid>(SaveReturn));
 }