public IActionResult Patch(string id, [FromBody] JsonPatchDocument <ClientCon> clientConPatch) { try { if (Guid.TryParse(id, out Guid parsedId)) { //var userId = User.FindFirstValue("sub"); ClientContact clientContact = _unitOfWork.ClientContacts.Get(parsedId); ClientCon clientCon = _mapper.Map <ClientCon>(clientContact); clientConPatch.ApplyTo(clientCon); _mapper.Map(clientCon, clientContact); _unitOfWork.Complete(); return(CreatedAtRoute("Get", new { id = _mapper.Map <ClientCon>(clientContact).Id }, _mapper.Map <ClientCon>(clientContact))); } } catch (Exception e) { string message = e.Message; } return(BadRequest()); }
public IActionResult Post([FromBody] ClientCon clientCon) { try { if (clientCon.Id == Guid.Empty) { ClientHeader clientHeader = _unitOfWork.ClientHeaders.SingleOrDefault(c => c.Id == clientCon.ClientHeaderId, true); if (clientHeader.Id != Guid.Empty) { if (!clientHeader.ClientContacts.Where(cc => cc.Email == clientCon.Email).Any()) { //var userId = User.FindFirstValue("sub"); var clientContact = _mapper.Map <ClientContact>(clientCon); _unitOfWork.ClientContacts.Add(clientContact); _unitOfWork.Complete(); return(Created(new Uri($"{Request.Path}/{clientContact.Id}", UriKind.Relative), _mapper.Map <ClientCon>(clientContact))); } } } } catch (Exception e) { string message = e.Message; } return(BadRequest()); }
private async Task <bool> CreateOrUpdateContactAsync(ClientCon clientCon, string apiAccessToken) { if (clientCon != null) { if (clientCon.Id != Guid.Empty) { var response = await _apiHelper.MakeAPICallAsync(apiAccessToken, HttpMethod.Get, APITypes.CLIENT, $"Contact/{clientCon.Id}"); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var foundClientCon = response.ContentAsType <ClientCon>(); if (foundClientCon != null) { if (!await _modelHelper.ProcessPatch(foundClientCon, clientCon, foundClientCon.Id, apiAccessToken, APITypes.CLIENT, "Contact")) { return(false); } } foreach (var clientAddr in clientCon.ClientAddresses) { if (!await CreateOrUpdateAddressAsync(clientAddr, apiAccessToken)) { return(false); } } } } else { HttpContent contentPost = new StringContent(JsonConvert.SerializeObject(clientCon), Encoding.UTF8, "application/json"); try { var response = await _apiHelper.MakeAPICallAsync(apiAccessToken, HttpMethod.Post, APITypes.CLIENT, $"Contact", contentPost); if (response.StatusCode != System.Net.HttpStatusCode.Created) { return(false); } } catch (Exception ex) { var responseString = $"Error calling API: {ex.Message}"; return(false); } } } return(true); }
//private static string GetStringBetween(string strSource, string strStartTag, string strEndTag) //{ // int start, end; // if(strSource.Contains(strStartTag) && strSource.Contains(strEndTag)) // { // start = strSource.IndexOf(strStartTag, 0) + strStartTag.Length; // end = strSource.IndexOf(strEndTag, start); // return strSource.Substring(start, end - start); // } // return string.Empty; //} private ClientHead GenerateDummyClientHeadModel() { var dummyClientHead = new ClientHead(); dummyClientHead.Id = Guid.Empty; var dummyClientCon = new ClientCon() { Id = Guid.Empty, FirstName = string.Empty, MiddleName = string.Empty, LastName = string.Empty, Email = string.Empty, Phone = string.Empty }; var dummyClientAddr = new ClientAddr() { ClientContactId = Guid.Parse("11111111-1111-1111-1111-111111111111"), Id = Guid.Empty, AddressLine1 = string.Empty, AddressLine2 = string.Empty, AddressLine3 = string.Empty, AddressLine4 = string.Empty, AddressLine5 = string.Empty, Country = string.Empty, Email1 = string.Empty, Email2 = string.Empty, Email3 = string.Empty, Phone1 = string.Empty, Phone2 = string.Empty, Phone3 = string.Empty, PostalCode = string.Empty, Region = string.Empty }; dummyClientCon.ClientAddresses.Add(dummyClientAddr); dummyClientHead.ClientContacts.Add(dummyClientCon); return(dummyClientHead); }
/// <summary> /// Used for receiving data. /// </summary> /// <param name="In">connection socket</param> static void RxThreadFunk(object In) { BinaryFormatter BFormat = new BinaryFormatter(); //this will be called by By start StartThreadListen ServerClient ServerClientCon = (ServerClient)In; Queue <LineSegment> Que = ServerClientCon.Parent.SendQue; //Queue<byte[]> Que = ServerClientCon.Parent.SendQue; //listening socket Socket ClientCon; ClientCon = ServerClientCon.CSocket; //server will drop the client, if there is no response for this // period of time (ms) ClientCon.ReceiveTimeout = 0; using (MemoryStream MS = new MemoryStream()) while (ServerClientCon.Listening) { // bte buffer for us to load to byte[] buffer = new byte[2000000]; // the number of bytes that we get back int BiteCount = 0; try { // get a byte BiteCount = ClientCon.Receive(buffer); ServerClientCon.Parent.RxBytCount += BiteCount; ServerClientCon.RxCount++; } catch { // Disconnect ServerClientCon.Parent.CallDisconnectClient(ServerClientCon); return; } if (BiteCount == 0) { ServerClientCon.Parent.CallDisconnectClient(ServerClientCon); return; } // why bother to Deserialize? // the last postion to use; long LastPos = 0; // a serailization fail reset point long CurrentTryPos = 0; try { LastPos = MS.Position; MS.Seek(0, SeekOrigin.End); MS.Write(buffer, 0, BiteCount); MS.Position = LastPos; } #if DEBUG catch (Exception ex) #else catch #endif { #if DEBUG Trace.WriteLine($"Datastream error: {ex.Message}"); ServerClientCon.Parent.CallDisconnectClient(ServerClientCon); return; #endif } // loop over the newly written data do { object check = null; //store the postion for rewind CurrentTryPos = MS.Position; LineSegment LS = null; try { //de serializes check = BFormat.Deserialize(MS); if (check is LineSegment) { LS = (LineSegment)check; } else { ///DONT GIVE ME BAD DATA DAM IT ot i disconect ServerClientCon.Parent.CallDisconnectClient(ServerClientCon); return; } ServerClientCon.Parent.RxSegCount++; lock (Que) Que.Enqueue(LS); // a full frame was processed // increment total received frames count ServerClientCon.FrameCount++; //lock (Client.SyncLock) // update the destack avg //ServerClientCon.DestackAvg = (float)Client.RxFrameCount / Client.RxCount; }// may loop for ever look int later <-------------------------------------------------------------------------- catch (SerializationException SE) { // if there is an error tell us about it but step back as it is prob fragmentaion MS.Position = CurrentTryPos; // update fragments count ServerClientCon.FragmentCount++; // leave the loop break; } } while (MS.Position < MS.Length); if (MS.Position == MS.Length) { //resset all buffers if able or done MS.Position = 0; MS.SetLength(0); } Thread.Sleep(0); } }
// Process (send to dest) a message from a client private void ProcessMsg(BinaryReader reader, ClientCon me) { var sendToKey = reader.ReadString(); var message = reader.ReadLenBytes(); lock (clients) { var found = false; foreach (var other in clients) { if (other.Pubkey == sendToKey) { found = true; other.Writer.Write("msgrecv"); other.Writer.Write(me.Name); other.Writer.WriteLenBytes(message); } } if (!found) { me.Writer.Write("usernotfound"); me.Writer.Write(sendToKey); } } }
// Process a `list` command // Use database if available, otherwise use currently connected users private void ProcessList(ClientCon me) { var list = ListUsers(); if (list == null) { lock (clients) { me.Writer.Write("listres"); me.Writer.Write(clients.Count); foreach (var other in clients) { me.Writer.Write(other.Name); me.Writer.Write(other.Pubkey); } } } else { me.Writer.Write("listres"); me.Writer.Write(list.Count); foreach (var user in list) { me.Writer.Write(user.Value); me.Writer.Write(user.Key); } } }
// Read a header of a message and process the corresponding command private void ProcessCmd(BinaryReader reader, ClientCon me) { var cmd = reader.ReadString(); if (string.Equals(cmd, "msg", StringComparison.OrdinalIgnoreCase)) { ProcessMsg(reader, me); } else if (string.Equals(cmd, "list", StringComparison.OrdinalIgnoreCase)) { ProcessList(me); } else { Console.WriteLine("Unknown command by client: " + cmd); } }