public void Get_byIdUserExists_shouldReturnUser() { // Arrange var repository = new DapperUserRepository(Connection, UsersTableName); var userId = Guid.NewGuid(); var appId = Guid.NewGuid(); var serviceId = Guid.NewGuid(); var createdAt = DateTime.Now; var userModel = new UserModel { UserId = userId, AppId = appId, ServiceId = serviceId, CreatedAt = createdAt, Enabled = true }; // Act var id = repository.Insert(userModel); // Act var actual = repository.Get(id); // Assert Assert.AreEqual(userId, actual.UserId); Assert.AreEqual(appId, actual.AppId); Assert.AreEqual(serviceId, actual.ServiceId); Assert.AreEqual(createdAt.Date, actual.CreatedAt.Date); Assert.IsTrue(actual.Enabled); Assert.IsTrue(actual.Id > 0); }
public void Update(long id, UserModel user) { if (Get(id) == null) { throw new ArgumentException("User not found: " + id); } _connection.Query( String.Format(@" UPDATE {0} SET UserId = @UserId, AppId = @AppId, ServiceId = @ServiceId, Enabled = @Enabled, CreatedAt = @CreatedAt WHERE id = @id", _tableName), new { id, user.UserId, user.ServiceId, user.AppId, user.Enabled, user.CreatedAt }); }
public long Insert(UserModel user) { return _connection.Query<long>(String.Format(@" DECLARE @InsertedRows AS TABLE (Id int); INSERT INTO {0} OUTPUT Inserted.Id INTO @InsertedRows VALUES (@UserId, @AppId, @ServiceId, @Enabled, @CreatedAt); SELECT Id FROM @InsertedRows;", _tableName), new { user.UserId, user.ServiceId, user.AppId, user.Enabled, user.CreatedAt }).Single(); }
protected bool Equals(UserModel other) { return Id == other.Id && UserId.Equals(other.UserId) && AppId.Equals(other.AppId) && ServiceId.Equals(other.ServiceId) && Enabled.Equals(other.Enabled) && CreatedAt.ToString(CultureInfo.InvariantCulture).Equals(other.CreatedAt.ToString(CultureInfo.InvariantCulture)); }