Beispiel #1
0
        public Response GetResponse(HttpListenerRequest request, string[] parameters)
        {
            int entryId;

            if (!int.TryParse(parameters[3], out entryId))
            {
                return(BasicResponseBuilder.GetBadRequest());
            }

            var clientId = int.Parse(parameters[1]);

            try
            {
                switch (request.HttpMethod)
                {
                case "GET":
                    var journalEntry = _journalEntryService.GetEntryById(clientId, entryId);

                    var journalEntryVm = JournalEntryViewModel.ConvertToViewModel(journalEntry);

                    var jsonBody =
                        JsonConvert.SerializeObject(journalEntryVm, new JsonApiSerializerSettings());

                    return(new Response(200, jsonBody));

                case "PUT":
                    var body             = new StreamReader(request.InputStream).ReadToEnd();
                    var updatedJournalVm = JsonConvert.DeserializeObject <JournalEntryViewModel>(body);
                    var updatedJournal   = JournalEntry.ConvertToJournalEntry(updatedJournalVm);
                    _journalEntryService.UpdateEntry(clientId, entryId, updatedJournal);
                    return(new Response(200, "Updated"));

                case "DELETE":
                    _journalEntryService.DeleteEntry(clientId, entryId);
                    return(new Response(200, "Deleted"));
                }
            }
            catch (JournalEntryNotFoundException e)
            {
                Console.WriteLine(e.Message);
                return(BasicResponseBuilder.GetNotFound());
            }
            catch (MissingJournalEntryAttributesException e)
            {
                Console.WriteLine(e.Message);
                return(BasicResponseBuilder.GetBadRequest());
            }

            return(BasicResponseBuilder.GetBadRequest());
        }
Beispiel #2
0
        public Response GetResponse(HttpListenerRequest request, string[] parameters)
        {
            try
            {
                var clientId = int.Parse(parameters[1]);

                switch (request.HttpMethod)
                {
                case "GET":
                    var client   = _clientService.GetClientById(clientId);
                    var clientVm = ClientViewModel.ConvertToViewModel(client);

                    var responseBody = JsonConvert.SerializeObject(clientVm, new JsonApiSerializerSettings());

                    return(new Response(200, responseBody));

                case "PUT":
                    var body            = new StreamReader(request.InputStream).ReadToEnd();
                    var updatedClientVm = JsonConvert.DeserializeObject <ClientViewModel>(body);
                    var updatedClient   = Client.ConvertToClient(updatedClientVm);

                    _clientService.UpdateClient(clientId, updatedClient);

                    return(new Response(200, "Updated"));

                case "DELETE":
                    _clientService.DeleteClient(clientId);
                    return(new Response(200, "Deleted"));
                }

                return(BasicResponseBuilder.GetBadRequest());
            }
            catch (ClientNotFoundException e)
            {
                Console.WriteLine(e.Message);
                return(new Response(404, e.Message));
            }
        }
        public Response GetResponse(HttpListenerRequest request, string[] parameters)
        {
            try
            {
                switch (request.HttpMethod)
                {
                case "GET":
                    var clients = _clientService.GetClients();

                    var clientViewModels = clients.Select(ClientViewModel.ConvertToViewModel).ToList();

                    return(new Response(200,
                                        JsonConvert.SerializeObject(clientViewModels, new JsonApiSerializerSettings())));


                case "POST":
                    var body            = new StreamReader(request.InputStream).ReadToEnd();
                    var clientViewModel = JsonConvert.DeserializeObject <ClientViewModel>(body);

                    var id = _clientService.AddClient(Client.ConvertToClient(clientViewModel));

                    var idVm = new IdViewModel {
                        Id = id
                    };

                    return(new Response(201, JsonConvert.SerializeObject(idVm, new JsonApiSerializerSettings())));
                }

                return(BasicResponseBuilder.GetBadRequest());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new Response(400, "todo"));
            }
        }