Skip to content

Commit 59e4309

Browse files
committed
Use interfaces as params for UploadPhoto()
1 parent f27b95f commit 59e4309

6 files changed

Lines changed: 43 additions & 20 deletions

File tree

Refresh.Database/GameDatabaseContext.Photos.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Refresh.Database.Models.Users;
44
using Refresh.Database.Models.Levels;
55
using Refresh.Database.Models.Photos;
6+
using Refresh.Database.Query;
7+
using Refresh.Database.Helpers;
68

79
namespace Refresh.Database;
810

@@ -26,7 +28,7 @@ public partial class GameDatabaseContext // Photos
2628
.Include(p => p.Subject4User)
2729
.Include(p => p.Subject4User!.Statistics);
2830

29-
public void UploadPhoto(SerializedPhoto photo, GameUser publisher)
31+
public void UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject> subjects, GameUser publisher, GameLevel? level)
3032
{
3133
GamePhoto newPhoto = new()
3234
{
@@ -36,33 +38,26 @@ public void UploadPhoto(SerializedPhoto photo, GameUser publisher)
3638
PlanHash = photo.PlanHash,
3739

3840
Publisher = publisher,
39-
LevelType = photo.Level?.Type ?? "",
40-
OriginalLevelId = photo.Level?.LevelId ?? 0,
41-
OriginalLevelName = photo.Level?.Title ?? "",
41+
Level = level,
42+
LevelType = photo.LevelSlotType,
43+
OriginalLevelId = photo.LevelId,
44+
OriginalLevelName = photo.LevelTitle,
4245

4346
TakenAt = DateTimeOffset.FromUnixTimeSeconds(Math.Clamp(photo.Timestamp, this._time.EarliestDate, this._time.TimestampSeconds)),
4447
PublishedAt = this._time.Now,
4548
};
4649

47-
GameLevel? level = null;
50+
float[] bounds = new float[PhotoHelper.SubjectBoundCount];
4851

49-
if (photo.Level?.Type is "user" or "developer")
50-
{
51-
level = this.GetLevelByIdAndType(photo.Level.Type, photo.Level.LevelId);
52-
newPhoto.Level = level;
53-
}
54-
55-
float[] bounds = new float[SerializedPhotoSubject.FloatCount];
56-
57-
List<GamePhotoSubject> gameSubjects = new(photo.PhotoSubjects.Count);
58-
foreach (SerializedPhotoSubject subject in photo.PhotoSubjects)
52+
List<GamePhotoSubject> gameSubjects = new(subjects.Count());
53+
foreach (IPhotoUploadSubject subject in subjects)
5954
{
6055
GameUser? subjectUser = null;
6156

6257
if (!string.IsNullOrEmpty(subject.Username))
6358
subjectUser = this.GetUserByUsername(subject.Username);
6459

65-
SerializedPhotoSubject.ParseBoundsList(subject.BoundsList, bounds);
60+
PhotoHelper.ParseBoundsList(subject.BoundsList, bounds);
6661

6762
gameSubjects.Add(new GamePhotoSubject(subjectUser, subject.DisplayName, bounds));
6863

Refresh.Database/Models/Photos/SerializedPhoto.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Xml.Serialization;
2+
using Refresh.Database.Query;
23

34
namespace Refresh.Database.Models.Photos;
45

56
#nullable disable
67

78
[XmlRoot("photo")]
89
[XmlType("photo")]
9-
public class SerializedPhoto
10+
public class SerializedPhoto : IPhotoUpload
1011
{
1112
[XmlAttribute("timestamp")]
1213
public long Timestamp { get; set; }
@@ -23,6 +24,9 @@ public class SerializedPhoto
2324
[XmlElement("plan")] public string PlanHash { get; set; }
2425

2526
[XmlElement("slot")] public SerializedPhotoLevel Level { get; set; }
27+
[XmlIgnore] public int LevelId => this.Level.LevelId;
28+
[XmlIgnore] public string LevelType => this.Level.Type;
29+
[XmlIgnore] public string LevelTitle => this.Level.Title;
2630

27-
[XmlArray("subjects")] public List<SerializedPhotoSubject> PhotoSubjects { get; set; }
31+
[XmlArray("subjects")] public List<SerializedPhotoSubject> PhotoSubjects { get; set; } = [];
2832
}

Refresh.Database/Models/Photos/SerializedPhotoSubject.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using System.Xml.Serialization;
2+
using Refresh.Database.Query;
23

34
namespace Refresh.Database.Models.Photos;
45

56
#nullable disable
67

78
[XmlRoot("subject")]
89
[XmlType("subject")]
9-
public class SerializedPhotoSubject
10+
public class SerializedPhotoSubject : IPhotoUploadSubject
1011
{
1112
[XmlElement("npHandle")]
1213
public string Username { get; set; }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Refresh.Database.Query;
2+
3+
public interface IPhotoUpload
4+
{
5+
int LevelId { get; }
6+
string LevelType { get; }
7+
string LevelTitle { get; }
8+
string SmallHash { get; }
9+
string MediumHash { get;}
10+
string LargeHash { get; }
11+
string PlanHash { get; }
12+
long Timestamp { get; }
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Refresh.Database.Query;
2+
3+
public interface IPhotoUploadSubject
4+
{
5+
string Username { get; }
6+
string DisplayName { get; }
7+
string BoundsList { get; }
8+
}

Refresh.Interfaces.Game/Endpoints/PhotoEndpoints.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public Response UploadPhoto(RequestContext context, SerializedPhoto body, GameDa
5858
return Unauthorized;
5959
}
6060

61-
database.UploadPhoto(body, user);
61+
GameLevel? level = database.GetLevelByIdAndType(body.Level.Type, body.Level.LevelId);
62+
63+
database.UploadPhoto(body, body.PhotoSubjects, user, level);
6264

6365
return OK;
6466
}

0 commit comments

Comments
 (0)