summaryrefslogtreecommitdiffstats
path: root/api/WhatApi/Seed.cs
blob: 0e8ab596a440fed278fc502e8f42afb3353230bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Bogus;
using Bogus.Locations;
using WhatApi.Tables;

namespace WhatApi;

public static class Seed
{
    public static void Full(Database db, Action<SeedOptions> seedOptions) {
        var opt = new SeedOptions();
        seedOptions.Invoke(opt);
        Content(db, opt);
    }

    public static void Content(Database db, SeedOptions opt) {
        var any = db.Places.Any();
        if (any) {
            if (!opt.ClearTables) return;
            db.Places.RemoveRange(db.Places.ToList());
            db.SaveChanges();
        }
        var faker = new Faker();
        for (var i = 0; i < 30; i++) {
            var ip = faker.Internet.IpAddress();
            var content = new Content() {
                Id = Guid.NewGuid(),
                Ip = ip,
                Mime = "image/jpeg",
                BlobId = new Guid("3ae0ea2d-851d-4e27-ad89-205822126395")
            };
            var location = faker.Location();
            var point = location.AreaCircle(59.91838, 10.73861, 30000);
            var place = new Place() {
                Location = new Point(new Coordinate(point.Longitude, point.Latitude)),
                Content = content
            };
            db.Places.Add(place);
        }

        db.SaveChanges();
    }
}

public class SeedOptions
{
    public bool ClearTables { get; set; }
}