public async Task <string> Save(ActivityDTO dto) { _log.Info($"Save for activity \'{dto?.Subject}\'; type is \'{dto?.GetType()}\'; ID is {dto?.ActivityID} (for user \'{dto?.Username}')"); try { using (var context = new ActivityRepository(new IngressContext())) { var activity = DTOToActivity.Get(dto); if (activity.ActivityID == 0) { context.Create(activity); } else { context.Update(activity); } await context.SaveChanges(); return(activity.ActivityID.ToString()); } } catch (Exception ex) { _log.Error(ex); throw; } }
public async Task <bool> SaveActivity(ActivityDTO item, CancellationToken token) { var uri = $"{_server}/Activities/Save"; Reporter.Track("SaveActivityCall", new Dictionary <string, string> { { "Type", item.GetType().ToString() }, { "Broker", item.BrokerName }, { "Subject", item.Subject }, { "Username", item.Username } }); // TypeNameHandling: nice little setting tells the serialiser to add the type of the object to the json // so when it is deserialised it, it deserialised to the right concrete class instead of the base class var json = JsonConvert.SerializeObject(item, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); var response = await _client.PostAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"), token); if (response.IsSuccessStatusCode) { var message = await response.Content.ReadAsStringAsync(); message = message.Replace("\"", ""); if (int.TryParse(message, out var newId)) { item.ActivityID = newId; return(true); } Reporter.Track("SaveActivityFailure", new Dictionary <string, string> { { "BadMessage", message } }); return(false); } Report(response.StatusCode.ToString()); return(false); }
public static Activity Get(ActivityDTO dto) { return(_fromDTO[dto.GetType()].Invoke(dto)); }