Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Commit - Front End Add and Delete, Skeletal Code for API #8

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
5 changes: 4 additions & 1 deletion BackEnd/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,7 @@ id_token.txt
pipeline/locust/.env

# Ignore auto-generated minified JS
js/*.min.js
js/*.min.js

# Ignore node modules
node_modules
51 changes: 45 additions & 6 deletions BackEnd/ContactsAPI/ContactsAPI/Controllers/ContactController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,36 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ContactsAPI.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ContactsAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ContactController : Controller
{
// GET: api/<controller>

private IEnumerable<Contact> Contacts = new[]{
new Contact{Id="1", Name="Name 1", Phone="+2 222 2222", Email="[email protected]"},
new Contact{Id="2", Name="Name 2", Phone="+1 111 1111", Email="[email protected]"},
new Contact{Id="3", Name="Name 3", Phone="+3 333 3333", Email="[email protected]"},
};

[HttpGet]
public Contact[] Get(){
Contact[] contacts = Contacts.ToArray();
return contacts;
}

[HttpGet("{id}")]
public Contact[] Get(String id){
Contact[] contacts = Contacts.Where(c => c.Id == id).ToArray();
return contacts;
}

/*// GET: api/<controller>
[HttpGet]
public IEnumerable<string> Get()
{
Expand All @@ -23,24 +44,42 @@ public IEnumerable<string> Get()
public string Get(int id)
{
return "value";
}
}*/

// POST api/<controller>
[HttpPost]
public void Post([FromBody]string value)
public Contact[] Post([FromBody] Contact contact)
{
Contact c = new Contact { Id = contact.Id, Name = contact.Name, Phone = contact.Phone, Email = contact.Email };
Contacts = Contacts.Append(c);

Contact[] contacts = Contacts.ToArray();
return contacts;
}

// PUT api/<controller>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
[HttpPut]
public Contact[] Put([FromBody] Contact contact)
{
List<Contact> contacts = Contacts.ToList();
var result = from r in contacts where r.Id == contact.Id select r;

result.First().Name = contact.Name;
result.First().Phone = contact.Phone;
result.First().Email = contact.Email;

Contact[] updatedContacts = contacts.ToArray();

return updatedContacts;
}

// DELETE api/<controller>/5
[HttpDelete("{id}")]
public void Delete(int id)
public Contact[] Delete(String id)
{
Contacts = Contacts.Where(c => c.Id != id).ToList();
Contact[] contacts = Contacts.ToArray();
return contacts;
}
}
}
5 changes: 4 additions & 1 deletion BackEnd/ContactsAPI/ContactsAPI/Models/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace ContactsAPI.Models
{
public class Contact
{
// Insert Contact Fields Here
public string Id {get; set;}
public string Name {get; set;}
public string Phone {get; set;}
public string Email {get; set;}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
"applicationUrl": "http://localhost:5001;http://localhost:5000"
}
}
}
22 changes: 22 additions & 0 deletions BackEnd/ContactsAPI/ContactsAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Cors.Infrastructure;

namespace ContactsAPI
{
Expand All @@ -28,6 +29,24 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

/*services.AddCors(options =>
{
options.AddPolicy("AllowAll", new CorsPolicyBuilder()
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.Build());
});*/

/*services.AddCors(options => options.AddPolicy("Default", x =>
{
x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
})); */

services.AddCors(options => options.AddPolicy(name: "AllowOrigin", builder => {
builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod();
}));

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
Expand Down Expand Up @@ -69,6 +88,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseRouting();


app.UseCors("AllowOrigin");

app.UseAuthorization();

app.UseEndpoints(endpoints =>
Expand Down
Binary file added contacts-app/.DS_Store
Binary file not shown.
Loading