// POST api/records public HistoryRecordViewModel Post([FromBody] HistoryRecordViewModel viewModelHistoryRecord) { if (ModelState.IsValid) { HistoryRecord historyRecord = Mapper.Map <HistoryRecordViewModel, HistoryRecord>(viewModelHistoryRecord); try { historyRecord.CreateDate = DateTime.Now; //todo настроить в EF автогенерацию historyRecordService.CreateHistoryRecord(historyRecord); historyRecordService.SaveHistoryRecord(); } catch (Exception ex) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); } ConverterUrl converter = new ConverterUrl(10); //todo вынести в настройки 10 string hash = converter.Encode(historyRecord.Id); //historyRecord.UrlShort = $"{Request.RequestUri.Authority}api/values/{hash}"; //to do Или проще так? historyRecord.UrlShort = $"{Request.RequestUri.Authority}/Home/Record/{hash}"; historyRecordService.UpdateHistoryRecord(historyRecord); historyRecordService.SaveHistoryRecord(); viewModelHistoryRecord = Mapper.Map <HistoryRecord, HistoryRecordViewModel>(historyRecord); } return(viewModelHistoryRecord); }
// GET api/records/5 public IHttpActionResult Get(string str) { if (String.IsNullOrEmpty(str)) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)); } ConverterUrl converter = new ConverterUrl(10); long recordId = converter.Decode(str); HistoryRecord historyRecord = historyRecordService.GetHistoryRecord(recordId); if (historyRecord == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); } historyRecord.HitCount++; historyRecordService.UpdateHistoryRecord(historyRecord); historyRecordService.SaveHistoryRecord(); //var response = Request.CreateResponse(HttpStatusCode.Moved); //response.Headers.Location = new Uri(historyRecord.UrlLong); return(Redirect(historyRecord.UrlLong)); }
public void Throw_Exc_When_Alphabet_Contains_Label() { //Arrange string alphabet = "abcdefghijklmnopqrstuvwxyz"; string label = "z"; ConverterUrl customConverter = new ConverterUrl(0, alphabet, label); }
public void Throw_Exc_When_Alphabet_Less_Than_16() { //Arrange string alphabet = "qwerty"; ConverterUrl customConverter = new ConverterUrl(0, alphabet); }
public ConverterUrlTest() { converter = new ConverterUrl(10); }