-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
sharp/content.Tests/repository/NotificationRepositoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace content.Tests.repository; | ||
|
||
using MySqlConnector; | ||
using Xunit; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using content.repository; | ||
|
||
public class NotificationRepositoryTests | ||
{ | ||
private readonly NotificationRepository _repository; | ||
|
||
public NotificationRepositoryTests() | ||
{ | ||
var dataSource = new MySqlDataSource(Environment.GetEnvironmentVariable("CONNECTION_STRING") !); | ||
_repository = new NotificationRepository(dataSource); | ||
} | ||
|
||
[Fact] | ||
public async Task FindByReceiverId_ReturnsNotifications_WhenUnreadOnlyIsFalse() | ||
{ | ||
// Arrange | ||
const long receiverId = 1L; | ||
const long page = 0L; | ||
const int size = 10; | ||
|
||
// Act | ||
var result = await _repository.FindByReceiverId(receiverId, page, size); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.IsType<List<Message>>(result); | ||
} | ||
|
||
[Fact] | ||
public async Task FindByReceiverId_ReturnsUnreadNotifications_WhenUnreadOnlyIsTrue() | ||
{ | ||
// Arrange | ||
const long receiverId = 1L; | ||
const long page = 0L; | ||
const int size = 10; | ||
const bool unreadOnly = true; | ||
|
||
// Act | ||
var result = await _repository.FindByReceiverId(receiverId, page, size, unreadOnly); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.IsType<List<Message>>(result); | ||
} | ||
|
||
[Fact] | ||
public async Task Save_ReturnsNotification_WhenInsertIsSuccessful() | ||
{ | ||
// Arrange | ||
var notification = new Message | ||
{ | ||
SenderId = 1L, | ||
ReceiverId = 2L, | ||
Content = "Test content", | ||
Type = 1, | ||
}; | ||
|
||
// Act | ||
var result = await _repository.Save(notification); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.IsType<Message>(result); | ||
} | ||
|
||
[Fact] | ||
public async Task UpdateStatus_UpdatesNotificationStatus() | ||
{ | ||
// Arrange | ||
const long id = 1L; | ||
const int status = 1; | ||
|
||
// Act | ||
await _repository.UpdateStatus(id, status); | ||
|
||
// Assert | ||
// No exception means the test passed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2023-2024 sixwaaaay. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
using Dapper; | ||
using MySqlConnector; | ||
|
||
namespace content.repository; | ||
|
||
public record Message | ||
{ | ||
public long Id { get; set; } | ||
public long SenderId { get; init; } | ||
public long ReceiverId { get; init; } | ||
public string Content { get; init; } = string.Empty; | ||
public short Type { get; init; } | ||
public short Status { get; init; } | ||
public DateTime CreatedAt { get; init; } = DateTime.Now; | ||
} | ||
|
||
public interface INotificationRepository | ||
{ | ||
ValueTask<IReadOnlyList<Message>> FindByReceiverId(long receiverId, long page, int size, bool unreadOnly); | ||
|
||
ValueTask<Message> Save(Message notification); | ||
ValueTask UpdateStatus(long id, short status); | ||
} | ||
|
||
public class NotificationRepository(MySqlDataSource dataSource) : INotificationRepository | ||
{ | ||
public async ValueTask<IReadOnlyList<Message>> FindByReceiverId(long receiverId, long page, int size, | ||
bool unreadOnly = false) | ||
{ | ||
await using var connection = await dataSource.OpenConnectionAsync(); | ||
if (unreadOnly) | ||
{ | ||
var messageNotifications = await connection.QueryAsync<Message>( | ||
"SELECT id, sender_id, receiver_id, content, type, status, created_at " + | ||
"FROM notifications WHERE receiver_id = @receiverId AND status = 0 " + | ||
"AND id > @page limit @size", new { receiverId, page, size }); | ||
return messageNotifications.ToList(); | ||
} | ||
|
||
var notifications = await connection.QueryAsync<Message>( | ||
"SELECT id, sender_id, receiver_id, content, type, status, created_at " + | ||
"FROM notifications WHERE receiver_id = @receiverId " + | ||
"AND id > @page limit @size", | ||
new { receiverId, page, size }); | ||
return notifications.ToList(); | ||
} | ||
|
||
public async ValueTask<Message> Save(Message notification) | ||
{ | ||
await using var connection = await dataSource.OpenConnectionAsync(); | ||
var result = await connection.ExecuteAsync( | ||
"INSERT INTO notifications (sender_id, receiver_id, content, type, status, created_at) " + | ||
"VALUES (@SenderId, @ReceiverId, @Content, @Type, @Status, @CreatedAt)", notification); | ||
if (result == 0) | ||
{ | ||
throw new Exception("Insert notification failed"); | ||
} | ||
|
||
notification.Id = await connection.QuerySingleAsync<long>("SELECT LAST_INSERT_ID()"); | ||
return notification; | ||
} | ||
|
||
public async ValueTask UpdateStatus(long id, short status) | ||
{ | ||
await using var connection = await dataSource.OpenConnectionAsync(); | ||
await connection.ExecuteAsync( | ||
"UPDATE notifications SET status = @status WHERE id = @id", new { id, status }); | ||
} | ||
} |