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

Create Class library and Web API #153

Merged
merged 38 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1ade773
Regenerate AP2.sln
Tom-stack3 Dec 7, 1999
703f9c3
Create .gitignore file
Tom-stack3 May 14, 2022
8ad3720
Create .gitignore for class-library
Tom-stack3 May 14, 2022
8d35a09
Create User, Message and Chat classes
Tom-stack3 May 14, 2022
2e69157
Create web-api project
Tom-stack3 May 14, 2022
9a370a8
Add reference to class-library project
Tom-stack3 May 14, 2022
b5acc4f
Change user's Chats property
Tom-stack3 May 14, 2022
f74358a
Create Invitation and Transfer classes
Tom-stack3 May 14, 2022
7a3584a
Create Contacts Controller
Tom-stack3 May 14, 2022
c9c86f1
Create Transfer and Invitation controllers
Tom-stack3 May 14, 2022
ef6084f
Remove Chat.Type property
Tom-stack3 May 14, 2022
4b3ee4a
Add comment to clarify
Tom-stack3 May 15, 2022
f6a6cfa
Change template text
Tom-stack3 May 15, 2022
67bd00e
Remove .gitignore
Tom-stack3 May 15, 2022
1cf270e
Use List instead of Dictionary in User class
Tom-stack3 May 16, 2022
f22f465
Create UsersService
Tom-stack3 May 16, 2022
ad86721
Minor change
Tom-stack3 May 16, 2022
4ed95a2
Make Update() return updated User or null
Tom-stack3 May 16, 2022
25eaea1
Use Dictionary instead of List in User class
Tom-stack3 May 16, 2022
2f322b4
Add Get list of usernames
Tom-stack3 May 16, 2022
23f2b7d
Create Chats Service
Tom-stack3 May 16, 2022
028db92
Remove redundant piece of code
Tom-stack3 May 16, 2022
e2188b5
Fix logical problem in Deletion
Tom-stack3 May 16, 2022
c73d832
Implement use of UsersService and ChatsService in ContactsController
Tom-stack3 May 16, 2022
c6a7b38
Create Solution at AP2
Tom-stack3 May 17, 2022
52712a0
Make chat id a string
Tom-stack3 May 17, 2022
2321a56
Fix build errors
Tom-stack3 May 17, 2022
02a178b
Fix nullable types
Tom-stack3 May 17, 2022
d43228a
Add Invitation controller implementation
Tom-stack3 May 17, 2022
611fbf4
Delete unused argument and functions
Tom-stack3 May 17, 2022
cd4af27
Add Transfer controller implementation
Tom-stack3 Aug 10, 2006
de56d9e
Fix argument parsing
Tom-stack3 May 18, 2022
279564e
Fix nullable warning
Tom-stack3 May 18, 2022
35ed14c
Create and use OuterMessage class
Tom-stack3 May 18, 2022
69e652c
Use JsonElement to parse arguments
Tom-stack3 May 19, 2022
f5eedc8
Create Services namespace
Tom-stack3 May 19, 2022
961a73e
Add comments
Tom-stack3 Aug 11, 2026
71be54b
Add dependency injection
Tom-stack3 Dec 7, 1970
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
Tom-stack3 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions class-library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
obj
bin
13 changes: 13 additions & 0 deletions class-library/Chat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace class_library;

public class Chat
{
[Key]
public int Id { get; set; }
[Required]
public ICollection<User> Members { get; set; }
[Required]
public ICollection<Message> Messages { get; set; }
}
13 changes: 13 additions & 0 deletions class-library/Invitation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace class_library;

public class Invitation
{
[Required]
public string From { get; set; }
[Required]
public string To { get; set; }
[Required]
public string Server { get; set; }
TomBenDor marked this conversation as resolved.
Show resolved Hide resolved
}
17 changes: 17 additions & 0 deletions class-library/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;

namespace class_library;

public class Message
{
[Key]
public int Id { get; set; }
[Required]
public string Sender { get; set; }
[Required]
public string Text { get; set; }
[Required]
public DateTime Timestamp { get; set; }
[Required]
public string type { get; set; }
}
13 changes: 13 additions & 0 deletions class-library/Transfer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;

namespace class_library;

public class Transfer
TomBenDor marked this conversation as resolved.
Show resolved Hide resolved
{
[Required]
public string From { get; set; }
[Required]
public string To { get; set; }
[Required]
public string Content { get; set; }
}
38 changes: 38 additions & 0 deletions class-library/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.ComponentModel.DataAnnotations;

namespace class_library;

public class User
{
// Username must contain only letters, numbers, and hyphens
[Key, RegularExpression(@"^[a-zA-Z0-9-]+$")]
// Username must be at least 3 characters long
[MinLength(3)]
public string Username { get; set; }

[Required]
// Password must be at least 6 characters long
[MinLength(6)]
// Password must contain at least one number, one lowercase and one uppercase character
[RegularExpression(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$")]
public string Password { get; set; }

[Required]
// Display name must be at least 3 characters long
[MinLength(3)]
// Display name can only contain letters, spaces, hyphens, periods, dots, and commas
[RegularExpression(@"^[a-zA-Z0-9- .,]+$")]
public string Name { get; set; }

[Required]
public string ProfilePicture { get; set; }

[Required]
public string Server { get; set; }

// Dictionary of User Id's as keys and chats as values
public IDictionary<string, Chat> Chats { get; set; }

// Dictionary of Chat Id's as keys and number of unread messages as values
public IDictionary<int, int> UnreadMessages { get; set; }
}
10 changes: 10 additions & 0 deletions class-library/class-library.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>class_library</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions web-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
obj/
98 changes: 98 additions & 0 deletions web-api/Controllers/ContactsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.AspNetCore.Mvc;
using class_library;

namespace web_api.Controllers;

[ApiController]
[Route("api/contacts")]

public class ContactsController : ControllerBase
{
// private readonly IUsersRepository _contactRepository;

// public ContactsController(IUsersRepository contactRepository)
// {
// _contactRepository = contactRepository;
// }

[HttpGet]
public IActionResult Get()
{
// return Ok(_contactRepository.GetAll());
return Ok("All Contacts of current user");
}

[HttpPost]
public IActionResult Post([FromBody] User contact)
{
// if (contact == null)
// {
// return BadRequest();
// }
// _contactRepository.Add(contact);
// return CreatedAtAction("Get", new { id = contact.Id }, contact);
return Ok("Create new Contact");
}

[HttpGet("{id}")]
public IActionResult Get(int id)
{
// var contact = _contactRepository.GetById(id);
// if (contact == null)
// {
// return NotFound();
// }
// return Ok(contact);
return Ok("Get Contact with id: " + id);
}

[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] User contact)
{
// if (contact == null || contact.Id != id)
// {
// return BadRequest();
// }
// var contactToUpdate = _contactRepository.GetById(id);
// if (contactToUpdate == null)
// {
// return NotFound();
// }
// _contactRepository.Update(contactToUpdate, contact);
// return NoContent();
return Ok("Update Contact with id: " + id);
}

[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
return Ok("Delete Contact with id: " + id);
}

[HttpGet("{id}/messages")]
public IActionResult GetMessages(int id)
{
return Ok("Get Messages of Contact with id: " + id);
}
[HttpPost("{id}/messages")]
public IActionResult PostNewMessage(int id, [FromBody] Message message)
{
return Ok("Create new Message sent to Contact with id: " + id);
}

[HttpGet("{id}/messages/{id2}")]
public IActionResult GetMessage(int id, int id2)
{
return Ok("Get Message with id: " + id2 + " sent to Contact with id: " + id);
}
[HttpPut("{id}/messages/{id2}")]
public IActionResult PutMessage(int id, int id2, [FromBody] Message message)
{
return Ok("Update Message with id: " + id2 + " sent to Contact with id: " + id);
}
[HttpDelete("{id}/messages/{id2}")]
public IActionResult DeleteMessage(int id, int id2)
{
return Ok("Delete Message with id: " + id2 + " sent to Contact with id: " + id);
}
}
16 changes: 16 additions & 0 deletions web-api/Controllers/InvitationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using class_library;

namespace web_api.Controllers;

[ApiController]
[Route("api/invitations")]

public class InvitationController : ControllerBase
{
[HttpPost]
public IActionResult Post([FromBody] Invitation invitation)
{
return Ok("Create new Invitation");
}
}
16 changes: 16 additions & 0 deletions web-api/Controllers/TransferController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using class_library;

namespace web_api.Controllers;

[ApiController]
[Route("api/transfer")]

public class TransferController : ControllerBase
{
[HttpPost]
public IActionResult Post([FromBody] Transfer transfer)
{
return Ok("Transfer new message to user on a remote server");
}
}
25 changes: 25 additions & 0 deletions web-api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
31 changes: 31 additions & 0 deletions web-api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:28545",
"sslPort": 44341
}
},
"profiles": {
"web_api": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7090;http://localhost:5295",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions web-api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions web-api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
18 changes: 18 additions & 0 deletions web-api/web-api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>web_api</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\class-library\class-library.csproj" />
</ItemGroup>

</Project>