Пример #1
0
        public async Task <ClientDto> UpdateClient(ClientDto client, string clientId)
        {
            using (CellularCompanyContext db = new CellularCompanyContext())
            {
                try
                {
                    if (client != null && clientId != null)
                    {
                        client.ClientId = clientId;
                        ClientEntity entity = client.ToModel();
                        db.Clients.Attach(entity);
                        foreach (var propName in db.Entry(entity).CurrentValues.PropertyNames)
                        {
                            if (propName != nameof(entity.ClientId))
                            {
                                db.Entry(entity).Property(propName).IsModified = true;
                            }
                        }
                        await db.SaveChangesAsync();

                        return(entity.ToDto());
                    }
                    return(null);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return(null);
                }
            }
        }
Пример #2
0
        public async Task <IActionResult> SaveClientAsync([FromBody] ClientDto clientDto)
        {
            try
            {
                var client   = clientDto.ToModel();
                var response = await _applicationService.SaveAsync(client);

                var webResponse = new SaveWebResponse <ClientDto>()
                                  .From(response);

                if (webResponse.IsSuccessful)
                {
                    return(Ok(webResponse));
                }

                return(BadRequest(webResponse.Errors));
            }
            catch (Exception ex)
            {
                var message = "Unable to save the client";
                Console.WriteLine(ex);
                _logManager.LogError(ex, message);
                return(BadRequest(message));
            }
        }
Пример #3
0
        public async Task <ClientDto> CreateClient(ClientDto client)
        {
            using (CellularCompanyContext db = new CellularCompanyContext())
            {
                try
                {
                    if (client != null && CheckIfClientIdAlreadyExist(client.ClientId))
                    {
                        ClientEntity entity = client.ToModel();
                        db.Clients.Add(entity);
                        await db.SaveChangesAsync();

                        return(entity.ToDto());
                    }
                    return(null);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    return(null);
                }
            }
        }