public async Task <string> Put(MunicipalityTax model) { var result = string.Empty; try { var clientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); } }; using var client = new HttpClient(clientHandler); var response = await client.PutAsync( apiUrl, new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json")); result = await response.Content.ReadAsStringAsync(); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }
public MunicipalityTax Create(string name, DateTime date, Period period, decimal tax) { var municipality = _municipalitiesRepository.GetAllItems().FirstOrDefault(a => a.Name == name); if (municipality == default(Municipality)) { municipality = new Municipality { Name = name }; } var municipalityTax = new MunicipalityTax { TaxPeriod = new TaxPeriod { DateFrom = date.Date, DateTo = date.AddPeriod(period), Period = period }, Municipality = municipality, Tax = tax }; return(_taxesRepository.AddOrUpdate(municipalityTax)); }
public async Task <MunicipalityTax> AddMunicipalityTaxes(MunicipalityTax municipalityTax, bool saveToDb = false) { var municipality = _context.Municapilities.Where(m => m.Name == municipalityTax.MunicipalityName).FirstOrDefault(); var tax = Tax.FromMunicipalityTax(municipalityTax); if (municipality == null) { _context.Municapilities.Add(new Municipality { Name = municipalityTax.MunicipalityName }); _context.SaveChanges(); municipality = _context.Municapilities.Where(m => m.Name == municipalityTax.MunicipalityName).FirstOrDefault(); } tax.MunicipalityId = municipality.Id; if (!TaxIsValid(tax)) { throw new Exception("Invalid tax"); } _context.Taxes.Add(tax); await _context.SaveChangesAsync(); municipalityTax.EndDate = tax.EndDate; return(municipalityTax); }
public ActionResult PostNewMunicipalityTax([FromBody] MunicipalityTax municipalityTax) { if (_municipalityTaxDatabaseAgent.InsertNewMunicipalityTax(municipalityTax)) { return(Created("MunicipalityTax", "Data was inserted")); } return(BadRequest("Data already exists")); }
public static Tax FromMunicipalityTax(MunicipalityTax municipalityTax) { // This looks ugly because of my chosen exception handling return(new Tax { Value = municipalityTax.Value, Type = municipalityTax.Type.Value, StartDate = municipalityTax.StartDate.Value, EndDate = municipalityTax.StartDate.Value.EndingDate(municipalityTax.Type.Value) }); }
private MunicipalityTax ParseTax(ref ExcelWorksheet workSheet, int row) { var tax = new MunicipalityTax(); tax.MunicipalityName = workSheet.Cells[row, 1].Value.ToString().Trim(); tax.Value = ParseValue(workSheet.Cells[row, 2].Value.ToString()); tax.Type = workSheet.Cells[row, 3].Value.ToString().StringToEnum <Tax.TaxType>(); tax.StartDate = Convert.ToDateTime(workSheet.Cells[row, 4].Value.ToString()); return(tax); }
public void MunicipalityTaxDatabaseAgent_EnteredDatesAreValid_DatesAreValid(DateTime dateFrom, DateTime dateTo) { var municipalityTax = new MunicipalityTax() { DateFrom = dateFrom }; var datesAreValidAttribute = new DatesAreValidAttribute(); var result = datesAreValidAttribute.GetValidationResult(dateTo, new ValidationContext(municipalityTax)); Assert.True(result == ValidationResult.Success); }
public void MunicipalityTaxDatabaseAgent_EnteredDatesAreValid_DatesAreEmpty(DateTime dateFrom, DateTime dateTo) { var municipalityTax = new MunicipalityTax() { DateFrom = dateFrom }; var datesAreValidAttribute = new DatesAreValidAttribute(); var result = datesAreValidAttribute.GetValidationResult(dateTo, new ValidationContext(municipalityTax)); Assert.True(result != ValidationResult.Success); Assert.Equal("DateTo and DateFrom must be filled in", result.ErrorMessage); }
public IActionResult Delete(int id) { MunicipalityTax municipalityTax = _dataRepository.Get(id); if (municipalityTax == null) { log.LogInformation("Unsuccessful DELETE because of wrong input ID"); return(NotFound("The tax record couldn't be found.")); } _dataRepository.Delete(municipalityTax); return(Ok("The tax record has been deleted")); }
public IActionResult Get(int id) { MunicipalityTax municipalityTax = _dataRepository.Get(id); if (municipalityTax == null) { log.LogInformation("Unsuccessful query - no records found with requested ID"); return(NotFound("The tax record couldn't be found.")); } return(Ok(municipalityTax)); }
public void MunicipalityTaxDatabaseAgent_EnteredDatesAreValid_DatesAreNotValid(DateTime dateFrom, DateTime dateTo) { var municipalityTax = new MunicipalityTax() { DateFrom = dateFrom }; var datesAreValidAttribute = new DatesAreValidAttribute(); var result = datesAreValidAttribute.GetValidationResult(dateTo, new ValidationContext(municipalityTax)); Assert.True(result != ValidationResult.Success); Assert.Equal("Only daily, weekly, monthly and yearly dates are allowed", result.ErrorMessage); }
public void CreateMunicipalityTax(MunicipalityTax municipalityTax) { dbContext.MunicipalityTaxes.Add(municipalityTax); try { dbContext.SaveChanges(); } catch (DbUpdateException e) { throw new MunicipalityTaxUpdateException("Could not create tax records in database. " + "This is most likely due to a record already existing in the database with the same municipality, period and start date", e); } }
public IActionResult Post([FromBody] MunicipalityTax municipalityTax) { if (municipalityTax == null) { log.LogInformation("Unsuccessful POST because of missing input parameters"); return(BadRequest("Tax record is not complete.")); } _dataRepository.Add(municipalityTax); return(CreatedAtRoute( "Get", new { Id = municipalityTax.ID }, municipalityTax)); }
public void UpdateMunicipalityTax(MunicipalityTax municipalityTax) { dbContext.MunicipalityTaxes.Update(municipalityTax); try { dbContext.SaveChanges(); } catch (DbUpdateConcurrencyException e) { throw new MunicipalityTaxUpdateException("Could not update tax record in database. " + "This is most likely due to no record existing in the database with the same municipality, period and start date", e); } }
public IActionResult Put(int id, [FromBody] MunicipalityTax municipalityTax) { if (municipalityTax == null) { log.LogInformation("Unsuccessful PUT because of missing input parameters"); return(BadRequest("Tax record is null.")); } MunicipalityTax municipalityTaxToUpdate = _dataRepository.Get(id); if (municipalityTaxToUpdate == null) { log.LogInformation("Unsuccessful PUT because of wrong input ID"); return(NotFound("The tax record couldn't be found.")); } _dataRepository.Update(municipalityTaxToUpdate, municipalityTax); return(Ok(municipalityTax)); }
public bool InsertNewMunicipalityTax(MunicipalityTax municipalityTax) { var result = _context.MunicipalityTax .FirstOrDefault(mt => mt.DateFrom.Date == municipalityTax.DateFrom.Date && mt.DateTo.Date == municipalityTax.DateTo.Date && string.Equals(mt.Municipality, municipalityTax.Municipality, StringComparison.InvariantCultureIgnoreCase) && mt.Priority == municipalityTax.Priority); if (result != null) { return(false); } _context.MunicipalityTax.Add(municipalityTax); _context.SaveChanges(); return(true); }
private MunicipalityTax ParseMunicipalityTax(string csvLine) { var csvValues = csvLine.Split(",").Select(v => v.Trim()).ToArray(); //Expected format municipality,period,start date,tax if (csvValues.Length != 4) { throw new FormatException("Line did not match the expected format: municipality,period,start date,tax"); } var municipalityTax = new MunicipalityTax(); municipalityTax.Municipality = csvValues[0]; municipalityTax.Period = ParsePeriod(csvValues[1]); municipalityTax.StartDate = ParseStartDate(csvValues[2]); municipalityTax.EndDate = endDateService.GetEndDate(municipalityTax.StartDate, municipalityTax.Period); municipalityTax.Tax = ParseTax(csvValues[3]); return(municipalityTax); }
public ActionResult UpdateTax([FromBody] MunicipalityTax municipalityTax) { if (!ModelState.IsValid) { return(BadRequest("The posted model is invalid")); } municipalityTax.EndDate = endDateService.GetEndDate(municipalityTax.StartDate, municipalityTax.Period); try { municipalityTaxRepository.UpdateMunicipalityTax(municipalityTax); } catch (MunicipalityTaxUpdateException e) { return(BadRequest(e.Message)); } return(Ok()); }
public async Task <IActionResult> Get() { var taxes = await _repository.AllAsync(); MunicipalityTax a = new MunicipalityTax { Id = Guid.NewGuid(), Name = "Vilnius", Daily = new DailyTax { Value = 0.2, Dates = new List <DateTime> { DateTime.Parse("2020.01.01") } } }; await _repository.AddAsync(a); if (taxes == null || !taxes.Any()) { return(NotFound("No tax data found")); } return(Ok(taxes)); }
private MunicipalityTax RemapEntity(MunicipalityTaxBody body) { var entity = new MunicipalityTax { Id = Guid.NewGuid(), Name = body.Name }; if (body.Weekly != null) { entity.Weekly = new PeriodicTax { Value = body.Weekly.Value, StartDate = DateTime.Parse(body.Weekly.StartDate), EndDate = DateTime.Parse(body.Weekly.EndDate) }; } if (body.Monthly != null) { entity.Monthly = new PeriodicTax { Value = body.Monthly.Value, StartDate = DateTime.Parse(body.Monthly.StartDate), EndDate = DateTime.Parse(body.Monthly.EndDate) }; } if (body.Yearly != null) { entity.Yearly = new PeriodicTax { Value = body.Yearly.Value, StartDate = DateTime.Parse(body.Yearly.StartDate), EndDate = DateTime.Parse(body.Yearly.EndDate) }; } if (body.Daily != null) { var listOfDates = body.Daily.Dates.ToList().Select(d => DateTime.Parse(d)); entity.Daily = new DailyTax { Value = body.Daily.Value, Dates = listOfDates }; } return(entity); }
public MunicipalityTax AddOrUpdate(MunicipalityTax municipalityTax) { _dbContext.Entry(municipalityTax).State = municipalityTax.MunicipalityId > 0 ? EntityState.Modified : EntityState.Added; try { _dbContext.SaveChanges(); } catch (DbEntityValidationException dbEx) { _logger.Error(dbEx, $"Failed to execute Entity Add Or Update: {typeof(MunicipalityTax).Name}."); foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); throw new Exception(validationError.ErrorMessage, dbEx); } } } return(municipalityTax); }
public async Task <IActionResult> Update([FromBody] MunicipalityTax tax) { if (tax == null) { return(BadRequest("No tax data provided")); } try { var updated = await _repository.UpdateAsync(tax); if (updated) { return(Ok()); } return(BadRequest("Data update was unsuccessful")); } catch (Exception e) { return(BadRequest($"Invalid Request Body Model. Error: {e.Message}")); } }
public MunicipalityTax Post(MunicipalityTax municipalityTax) { return(_municipalityTaxesService.AddOrUpdate(municipalityTax)); }
public async Task <bool> UpdateAsync(MunicipalityTax entity) { var result = await _context.Taxes.ReplaceOneAsync(b => b.Id == entity.Id, entity); return(result.IsAcknowledged && result.ModifiedCount > 0); }
public Task Delete(MunicipalityTax entity) { throw new NotImplementedException(); }
public MunicipalityTax AddOrUpdate(MunicipalityTax municipalityTax) { return(_taxesRepository.AddOrUpdate(municipalityTax)); }
public async Task AddAsync(MunicipalityTax entity) { await _context.Taxes.InsertOneAsync(entity); }